import{_ as l,o as p,c as o,k as s,a,X as n}from"./chunks/framework.6e839c56.js";const k=JSON.parse('{"title":"Maputil","description":"","frontmatter":{},"headers":[],"relativePath":"api/packages/maputil.md","filePath":"api/packages/maputil.md"}'),e={name:"api/packages/maputil.md"},t=s("h1",{id:"Maputil",tabindex:"-1"},[a("Maputil "),s("a",{class:"header-anchor",href:"#Maputil","aria-label":'Permalink to "Maputil"'},"​")],-1),c=s("p",null,"maputil 包包括一些操作 map 的函数。",-1),r=s("div",{STYLE:"page-break-after: always;"},null,-1),y=s("h2",{id:"源码-",tabindex:"-1"},[a("源码: "),s("a",{class:"header-anchor",href:"#源码-","aria-label":'Permalink to "源码:"'},"​")],-1),F=s("ul",null,[s("li",null,[s("a",{href:"https://github.com/duke-git/lancet/blob/main/maputil/map.go",target:"_blank",rel:"noreferrer"},"https://github.com/duke-git/lancet/blob/main/maputil/map.go")])],-1),i=s("div",{STYLE:"page-break-after: always;"},null,-1),E=n(`<h2 id="用法-" tabindex="-1">用法: <a class="header-anchor" href="#用法-" aria-label="Permalink to &quot;用法:&quot;">​</a></h2><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span></code></pre></div>`,2),A=s("div",{STYLE:"page-break-after: always;"},null,-1),u=n('<h2 id="目录-" tabindex="-1">目录: <a class="header-anchor" href="#目录-" aria-label="Permalink to &quot;目录:&quot;">​</a></h2><ul><li><a href="#MapTo">MapTo</a></li><li><a href="#ForEach">ForEach</a></li><li><a href="#Filter">Filter</a></li><li><a href="#FilterByKeys">FilterByKeys</a></li><li><a href="#FilterByValues">FilterByValues</a></li><li><a href="#OmitBy">OmitBy</a></li><li><a href="#OmitByKeys">OmitByKeys</a></li><li><a href="#OmitByValues">OmitByValues</a></li><li><a href="#Intersect">Intersect</a></li><li><a href="#Keys">Keys</a></li><li><a href="#Values">Values</a></li><li><a href="#KeysBy">KeysBy</a></li><li><a href="#ValuesBy">ValuesBy</a></li><li><a href="#MapKeys">MapKeys</a></li><li><a href="#MapValues">MapValues</a></li><li><a href="#Entries">Entries</a></li><li><a href="#FromEntries">FromEntries</a></li><li><a href="#Transform">Transform</a></li><li><a href="#Merge">Merge</a></li><li><a href="#Minus">Minus</a></li><li><a href="#IsDisjoint">IsDisjoint</a></li><li><a href="#HasKey">HasKey</a></li><li><a href="#NewConcurrentMap">NewConcurrentMap</a></li><li><a href="#ConcurrentMap_Get">ConcurrentMap_Get</a></li><li><a href="#ConcurrentMap_Set">ConcurrentMap_Set</a></li><li><a href="#ConcurrentMap_GetOrSet">ConcurrentMap_GetOrSet</a></li><li><a href="#ConcurrentMap_Delete">ConcurrentMap_Delete</a></li><li><a href="#ConcurrentMap_GetAndDelete">ConcurrentMap_GetAndDelete</a></li><li><a href="#ConcurrentMap_Has">ConcurrentMap_Has</a></li><li><a href="#ConcurrentMap_Range">ConcurrentMap_Range</a></li></ul>',2),B=s("div",{STYLE:"page-break-after: always;"},null,-1),m=n(`<link rel="stylesheet" type="text/css" href="/styles/api_doc.css"><h2 id="API-文档-" tabindex="-1">API 文档: <a class="header-anchor" href="#API-文档-" aria-label="Permalink to &quot;API 文档:&quot;">​</a></h2><h3 id="MapTo" tabindex="-1"><span id="MapTo">MapTo</span> <a class="header-anchor" href="#MapTo" aria-label="Permalink to &quot;&lt;span id=&quot;MapTo&quot;&gt;MapTo&lt;/span&gt;&quot;">​</a></h3><p>快速将map或者其他类型映射到结构体或者指定类型。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">MapTo</span><span style="color:#F6F6F4;">(src any, dst any) </span><span style="color:#97E1F1;font-style:italic;">error</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">MapTo</span><span style="color:#999999;">(</span><span style="color:#393A34;">src any</span><span style="color:#999999;">,</span><span style="color:#393A34;"> dst any</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">error</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/4K7KBEPgS5M" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">        Person </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">            Name  </span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">  </span><span style="color:#DEE492;">\`</span><span style="color:#E7EE98;">json:&quot;name&quot;</span><span style="color:#DEE492;">\`</span></span>
<span class="line"><span style="color:#F6F6F4;">            Age   </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">     </span><span style="color:#DEE492;">\`</span><span style="color:#E7EE98;">json:&quot;age&quot;</span><span style="color:#DEE492;">\`</span></span>
<span class="line"><span style="color:#F6F6F4;">            Phone </span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">  </span><span style="color:#DEE492;">\`</span><span style="color:#E7EE98;">json:&quot;phone&quot;</span><span style="color:#DEE492;">\`</span></span>
<span class="line"><span style="color:#F6F6F4;">            Addr  Address </span><span style="color:#DEE492;">\`</span><span style="color:#E7EE98;">json:&quot;address&quot;</span><span style="color:#DEE492;">\`</span></span>
<span class="line"><span style="color:#F6F6F4;">        }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">        Address </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">            Street </span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;"> </span><span style="color:#DEE492;">\`</span><span style="color:#E7EE98;">json:&quot;street&quot;</span><span style="color:#DEE492;">\`</span></span>
<span class="line"><span style="color:#F6F6F4;">            Number </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">\`</span><span style="color:#E7EE98;">json:&quot;number&quot;</span><span style="color:#DEE492;">\`</span></span>
<span class="line"><span style="color:#F6F6F4;">        }</span></span>
<span class="line"><span style="color:#F6F6F4;">    )</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    personInfo </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#F286C4;">interface</span><span style="color:#F6F6F4;">{}{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">name</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">:  </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">Nothin</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">age</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">:   </span><span style="color:#BF9EEE;">28</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">phone</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">123456789</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">address</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#F286C4;">interface</span><span style="color:#F6F6F4;">{}{</span></span>
<span class="line"><span style="color:#F6F6F4;">            </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">street</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">test</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">            </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">number</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        },</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">var</span><span style="color:#F6F6F4;"> p Person</span></span>
<span class="line"><span style="color:#F6F6F4;">    err </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">MapTo</span><span style="color:#F6F6F4;">(personInfo, </span><span style="color:#F286C4;">&amp;</span><span style="color:#F6F6F4;">p)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(err)</span></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(p)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// &lt;nil&gt;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// {Nothin 28 123456789 {test 1}}</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">type</span><span style="color:#393A34;"> (</span></span>
<span class="line"><span style="color:#393A34;">        Person </span><span style="color:#1E754F;">struct</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">            Name  </span><span style="color:#AB5959;">string</span><span style="color:#393A34;">  </span><span style="color:#B5695999;">\`</span><span style="color:#B56959;">json:&quot;name&quot;</span><span style="color:#B5695999;">\`</span></span>
<span class="line"><span style="color:#393A34;">            Age   </span><span style="color:#AB5959;">int</span><span style="color:#393A34;">     </span><span style="color:#B5695999;">\`</span><span style="color:#B56959;">json:&quot;age&quot;</span><span style="color:#B5695999;">\`</span></span>
<span class="line"><span style="color:#393A34;">            Phone </span><span style="color:#AB5959;">string</span><span style="color:#393A34;">  </span><span style="color:#B5695999;">\`</span><span style="color:#B56959;">json:&quot;phone&quot;</span><span style="color:#B5695999;">\`</span></span>
<span class="line"><span style="color:#393A34;">            Addr  Address </span><span style="color:#B5695999;">\`</span><span style="color:#B56959;">json:&quot;address&quot;</span><span style="color:#B5695999;">\`</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">        Address </span><span style="color:#1E754F;">struct</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">            Street </span><span style="color:#AB5959;">string</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">\`</span><span style="color:#B56959;">json:&quot;street&quot;</span><span style="color:#B5695999;">\`</span></span>
<span class="line"><span style="color:#393A34;">            Number </span><span style="color:#AB5959;">int</span><span style="color:#393A34;">    </span><span style="color:#B5695999;">\`</span><span style="color:#B56959;">json:&quot;number&quot;</span><span style="color:#B5695999;">\`</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    )</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">personInfo</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#1E754F;">interface</span><span style="color:#999999;">{}{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">name</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;">  </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">Nothin</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">age</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;">   </span><span style="color:#2F798A;">28</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">phone</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">123456789</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">address</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#1E754F;">interface</span><span style="color:#999999;">{}{</span></span>
<span class="line"><span style="color:#393A34;">            </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">street</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">test</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">            </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">number</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">},</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">var</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">p</span><span style="color:#393A34;"> Person</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">err</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#998418;">MapTo</span><span style="color:#999999;">(</span><span style="color:#393A34;">personInfo</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">&amp;</span><span style="color:#393A34;">p</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">err</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">p</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// &lt;nil&gt;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// {Nothin 28 123456789 {test 1}}</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="ForEach" tabindex="-1"><span id="ForEach">ForEach</span> <a class="header-anchor" href="#ForEach" aria-label="Permalink to &quot;&lt;span id=&quot;ForEach&quot;&gt;ForEach&lt;/span&gt;&quot;">​</a></h3><p>对map中的每对key和value执行iteratee函数</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> ForEach[K comparable, V any](m </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V, iteratee </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(key K, value V))</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> ForEach</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">](</span><span style="color:#393A34;">m </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">,</span><span style="color:#393A34;"> iteratee </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">key K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> value V</span><span style="color:#999999;">))</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/OaThj6iNVXK" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">var</span><span style="color:#F6F6F4;"> sum </span><span style="color:#97E1F1;font-style:italic;">int</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    maputil.</span><span style="color:#97E1F1;">ForEach</span><span style="color:#F6F6F4;">(m, </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(_ </span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">, value </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) {</span></span>
<span class="line"><span style="color:#F6F6F4;">        sum </span><span style="color:#F286C4;">+=</span><span style="color:#F6F6F4;"> value</span></span>
<span class="line"><span style="color:#F6F6F4;">    })</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(sum)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 10</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">var</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">sum</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">ForEach</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">_ </span><span style="color:#AB5959;">string</span><span style="color:#999999;">,</span><span style="color:#393A34;"> value </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        sum </span><span style="color:#999999;">+=</span><span style="color:#393A34;"> value</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">sum</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 10</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Filter" tabindex="-1"><span id="Filter">Filter</span> <a class="header-anchor" href="#Filter" aria-label="Permalink to &quot;&lt;span id=&quot;Filter&quot;&gt;Filter&lt;/span&gt;&quot;">​</a></h3><p>迭代map中的每对key和value, 返回符合predicate函数的key, value。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> Filter[K comparable, V any](m </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V, predicate </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(key K, value V) </span><span style="color:#97E1F1;font-style:italic;">bool</span><span style="color:#F6F6F4;">) </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> Filter</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">](</span><span style="color:#393A34;">m </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">,</span><span style="color:#393A34;"> predicate </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">key K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> value V</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">bool</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/fSvF3wxuNG7" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">e</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    isEven </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(_ </span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">, value </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) </span><span style="color:#97E1F1;font-style:italic;">bool</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> value</span><span style="color:#F286C4;">%</span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">==</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    maputil.</span><span style="color:#97E1F1;">Filter</span><span style="color:#F6F6F4;">(m, </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(_ </span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">, value </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) {</span></span>
<span class="line"><span style="color:#F6F6F4;">        sum </span><span style="color:#F286C4;">+=</span><span style="color:#F6F6F4;"> value</span></span>
<span class="line"><span style="color:#F6F6F4;">    })</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">Filter</span><span style="color:#F6F6F4;">(m, isEven)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// map[b:2 d:4]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">e</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">isEven</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">_ </span><span style="color:#AB5959;">string</span><span style="color:#999999;">,</span><span style="color:#393A34;"> value </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">bool</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> value</span><span style="color:#AB5959;">%</span><span style="color:#2F798A;">2</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">==</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">Filter</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">_ </span><span style="color:#AB5959;">string</span><span style="color:#999999;">,</span><span style="color:#393A34;"> value </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        sum </span><span style="color:#999999;">+=</span><span style="color:#393A34;"> value</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#998418;">Filter</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">,</span><span style="color:#393A34;"> isEven</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// map[b:2 d:4]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="FilterByKeys" tabindex="-1"><span id="FilterByKeys">FilterByKeys</span> <a class="header-anchor" href="#FilterByKeys" aria-label="Permalink to &quot;&lt;span id=&quot;FilterByKeys&quot;&gt;FilterByKeys&lt;/span&gt;&quot;">​</a></h3><p>迭代map, 返回一个新map，其key都是给定的key值。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> FilterByKeys[K comparable, V any](m </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V, keys []K) </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> FilterByKeys</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">](</span><span style="color:#393A34;">m </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">,</span><span style="color:#393A34;"> keys </span><span style="color:#999999;">[]</span><span style="color:#393A34;">K</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/7ov6BJHbVqh" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">e</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">FilterByKeys</span><span style="color:#F6F6F4;">(m, []</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">{</span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// map[a:1 b:2]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">e</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">FilterByKeys</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">string</span><span style="color:#999999;">{</span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// map[a:1 b:2]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="FilterByValues" tabindex="-1"><span id="FilterByValues">FilterByValues</span> <a class="header-anchor" href="#FilterByValues" aria-label="Permalink to &quot;&lt;span id=&quot;FilterByValues&quot;&gt;FilterByValues&lt;/span&gt;&quot;">​</a></h3><p>迭代map, 返回一个新map，其value都是给定的value值。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> FilterByValues[K comparable, V comparable](m </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V, values []V) </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> FilterByValues</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V comparable</span><span style="color:#999999;">](</span><span style="color:#393A34;">m </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">,</span><span style="color:#393A34;"> values </span><span style="color:#999999;">[]</span><span style="color:#393A34;">V</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/P3-9MdcXegR" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">e</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">FilterByValues</span><span style="color:#F6F6F4;">(m, []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// map[c:3 d:4]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">e</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">FilterByValues</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// map[c:3 d:4]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="OmitBy" tabindex="-1"><span id="OmitBy">OmitBy</span> <a class="header-anchor" href="#OmitBy" aria-label="Permalink to &quot;&lt;span id=&quot;OmitBy&quot;&gt;OmitBy&lt;/span&gt;&quot;">​</a></h3><p>Filter的反向操作, 迭代map中的每对key和value, 删除符合predicate函数的key, value, 返回新map。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> OmitBy[K comparable, V any](m </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V, predicate </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(key K, value V) </span><span style="color:#97E1F1;font-style:italic;">bool</span><span style="color:#F6F6F4;">) </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> OmitBy</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">](</span><span style="color:#393A34;">m </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">,</span><span style="color:#393A34;"> predicate </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">key K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> value V</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">bool</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/YJM4Hj5hNwm" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">e</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    isEven </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(_ </span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">, value </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) </span><span style="color:#97E1F1;font-style:italic;">bool</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> value</span><span style="color:#F286C4;">%</span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">==</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">OmitBy</span><span style="color:#F6F6F4;">(m, isEven)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// map[a:1 c:3 e:5]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">e</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">isEven</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">_ </span><span style="color:#AB5959;">string</span><span style="color:#999999;">,</span><span style="color:#393A34;"> value </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">bool</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> value</span><span style="color:#AB5959;">%</span><span style="color:#2F798A;">2</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">==</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">OmitBy</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">,</span><span style="color:#393A34;"> isEven</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// map[a:1 c:3 e:5]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="OmitByKeys" tabindex="-1"><span id="OmitByKeys">OmitByKeys</span> <a class="header-anchor" href="#OmitByKeys" aria-label="Permalink to &quot;&lt;span id=&quot;OmitByKeys&quot;&gt;OmitByKeys&lt;/span&gt;&quot;">​</a></h3><p>FilterByKeys的反向操作, 迭代map, 返回一个新map，其key不包括给定的key值。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> OmitByKeys[K comparable, V any](m </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V, keys []K) </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> OmitByKeys</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">](</span><span style="color:#393A34;">m </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">,</span><span style="color:#393A34;"> keys </span><span style="color:#999999;">[]</span><span style="color:#393A34;">K</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/jXGrWDBfSRp" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">e</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">OmitByKeys</span><span style="color:#F6F6F4;">(m, []</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">{</span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// map[c:3 d:4 e:5]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">e</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">OmitByKeys</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">string</span><span style="color:#999999;">{</span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// map[c:3 d:4 e:5]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="OmitByValues" tabindex="-1"><span id="OmitByValues">OmitByValues</span> <a class="header-anchor" href="#OmitByValues" aria-label="Permalink to &quot;&lt;span id=&quot;OmitByValues&quot;&gt;OmitByValues&lt;/span&gt;&quot;">​</a></h3><p>FilterByValues的反向操作, 迭代map, 返回一个新map，其value不包括给定的value值。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> OmitByValues[K comparable, V comparable](m </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V, values []V) </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> OmitByValues</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V comparable</span><span style="color:#999999;">](</span><span style="color:#393A34;">m </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">,</span><span style="color:#393A34;"> values </span><span style="color:#999999;">[]</span><span style="color:#393A34;">V</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/XB7Y10uw20_U" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">e</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">OmitByValues</span><span style="color:#F6F6F4;">(m, []</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">, </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// map[a:1 b:2 c:3]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">4</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">e</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">OmitByValues</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span><span style="color:#2F798A;">4</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// map[a:1 b:2 c:3]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Intersect" tabindex="-1"><span id="Intersect">Intersect</span> <a class="header-anchor" href="#Intersect" aria-label="Permalink to &quot;&lt;span id=&quot;Intersect&quot;&gt;Intersect&lt;/span&gt;&quot;">​</a></h3><p>多个map的交集操作</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> Intersect[K comparable, V any](maps </span><span style="color:#F286C4;">...map</span><span style="color:#F6F6F4;">[K]V) </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> Intersect</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">](</span><span style="color:#393A34;">maps </span><span style="color:#AB5959;">...</span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/Zld0oj3sjcC" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m1 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    m2 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">6</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">7</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    m3 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">9</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">e</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">9</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result1 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">Intersect</span><span style="color:#F6F6F4;">(m1)</span></span>
<span class="line"><span style="color:#F6F6F4;">    result2 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">Intersect</span><span style="color:#F6F6F4;">(m1, m2)</span></span>
<span class="line"><span style="color:#F6F6F4;">    result3 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">Intersect</span><span style="color:#F6F6F4;">(m1, m2, m3)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result1)</span></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result2)</span></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result3)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// map[a:1 b:2 c:3]</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// map[a:1 b:2]</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// map[a:1]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m1</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m2</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">6</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">7</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m3</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">9</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">e</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">9</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result1</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">Intersect</span><span style="color:#999999;">(</span><span style="color:#393A34;">m1</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result2</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">Intersect</span><span style="color:#999999;">(</span><span style="color:#393A34;">m1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> m2</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result3</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">Intersect</span><span style="color:#999999;">(</span><span style="color:#393A34;">m1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> m2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> m3</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result1</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result2</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result3</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// map[a:1 b:2 c:3]</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// map[a:1 b:2]</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// map[a:1]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Keys" tabindex="-1"><span id="Keys">Keys</span> <a class="header-anchor" href="#Keys" aria-label="Permalink to &quot;&lt;span id=&quot;Keys&quot;&gt;Keys&lt;/span&gt;&quot;">​</a></h3><p>返回map中所有key的切片</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> Keys[K comparable, V any](m </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V) []K</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> Keys</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">](</span><span style="color:#393A34;">m </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#393A34;">K</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/xNB5bTb97Wd" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    keys </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">Keys</span><span style="color:#F6F6F4;">(m)</span></span>
<span class="line"><span style="color:#F6F6F4;">    sort.</span><span style="color:#97E1F1;">Ints</span><span style="color:#F6F6F4;">(keys)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(keys)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// [1 2 3 4 5]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">]</span><span style="color:#AB5959;">string</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">1</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">2</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">3</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">4</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">5</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">keys</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">Keys</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    sort</span><span style="color:#999999;">.</span><span style="color:#998418;">Ints</span><span style="color:#999999;">(</span><span style="color:#393A34;">keys</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">keys</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// [1 2 3 4 5]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Merge" tabindex="-1"><span id="Merge">Merge</span> <a class="header-anchor" href="#Merge" aria-label="Permalink to &quot;&lt;span id=&quot;Merge&quot;&gt;Merge&lt;/span&gt;&quot;">​</a></h3><p>合并多个maps, 相同的key会被后来的key覆盖</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> Merge[K comparable, V any](maps </span><span style="color:#F286C4;">...map</span><span style="color:#F6F6F4;">[K]V) </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> Merge</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">](</span><span style="color:#393A34;">maps </span><span style="color:#AB5959;">...</span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/H95LENF1uB-" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m1 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    m2 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">1</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">2</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">Merge</span><span style="color:#F6F6F4;">(m1, m2)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// map[1:c 2:b 3:d]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m1</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">]</span><span style="color:#AB5959;">string</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">1</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">2</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m2</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">]</span><span style="color:#AB5959;">string</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">1</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">1</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">3</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">2</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">Merge</span><span style="color:#999999;">(</span><span style="color:#393A34;">m1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> m2</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// map[1:c 2:b 3:d]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Minus" tabindex="-1"><span id="Minus">Minus</span> <a class="header-anchor" href="#Minus" aria-label="Permalink to &quot;&lt;span id=&quot;Minus&quot;&gt;Minus&lt;/span&gt;&quot;">​</a></h3><p>返回一个map，其中的key存在于mapA，不存在于mapB.</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> Minus[K comparable, V any](mapA, mapB </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V) </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> Minus</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">](</span><span style="color:#393A34;">mapA</span><span style="color:#999999;">,</span><span style="color:#393A34;"> mapB </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/3u5U9K7YZ9m" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m1 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    m2 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">11</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">22</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">33</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">Minus</span><span style="color:#F6F6F4;">(m1, m2)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// map[c:3]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m1</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m2</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">11</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">22</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">33</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">Minus</span><span style="color:#999999;">(</span><span style="color:#393A34;">m1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> m2</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// map[c:3]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Values" tabindex="-1"><span id="Values">Values</span> <a class="header-anchor" href="#Values" aria-label="Permalink to &quot;&lt;span id=&quot;Values&quot;&gt;Values&lt;/span&gt;&quot;">​</a></h3><p>返回map中所有value的切片</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> Values[K comparable, V any](m </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V) []V</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> Values</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">](</span><span style="color:#393A34;">m </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#393A34;">V</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/CBKdUc5FTW6" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">4</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    values </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">Values</span><span style="color:#F6F6F4;">(m)</span></span>
<span class="line"><span style="color:#F6F6F4;">    sort.</span><span style="color:#97E1F1;">Strings</span><span style="color:#F6F6F4;">(values)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// [a a b c d]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">]</span><span style="color:#AB5959;">string</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">1</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">2</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">3</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">4</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">5</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">values</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">Values</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    sort</span><span style="color:#999999;">.</span><span style="color:#998418;">Strings</span><span style="color:#999999;">(</span><span style="color:#393A34;">values</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// [a a b c d]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="KeysBy" tabindex="-1"><span id="KeysBy">KeysBy</span> <a class="header-anchor" href="#KeysBy" aria-label="Permalink to &quot;&lt;span id=&quot;KeysBy&quot;&gt;KeysBy&lt;/span&gt;&quot;">​</a></h3><p>创建一个切片，其元素是每个map的key调用mapper函数的结果。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> KeysBy[K comparable, V any, T any](m </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V, mapper </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(item K) T) []T</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> KeysBy</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">,</span><span style="color:#393A34;"> T any</span><span style="color:#999999;">](</span><span style="color:#393A34;">m </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">,</span><span style="color:#393A34;"> mapper </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">item K</span><span style="color:#999999;">)</span><span style="color:#393A34;"> T</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#393A34;">T</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/hI371iB8Up8" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">sort</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    keys </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">KeysBy</span><span style="color:#F6F6F4;">(m, </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(n </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> n </span><span style="color:#F286C4;">+</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">1</span></span>
<span class="line"><span style="color:#F6F6F4;">    })</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    sort.</span><span style="color:#97E1F1;">Ints</span><span style="color:#F6F6F4;">(keys)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(keys)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// [2 3 4]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">sort</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">]</span><span style="color:#AB5959;">string</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">1</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">2</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">3</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">keys</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">KeysBy</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">n </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> n </span><span style="color:#AB5959;">+</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    sort</span><span style="color:#999999;">.</span><span style="color:#998418;">Ints</span><span style="color:#999999;">(</span><span style="color:#393A34;">keys</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">keys</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// [2 3 4]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="ValuesBy" tabindex="-1"><span id="ValuesBy">ValuesBy</span> <a class="header-anchor" href="#ValuesBy" aria-label="Permalink to &quot;&lt;span id=&quot;ValuesBy&quot;&gt;ValuesBy&lt;/span&gt;&quot;">​</a></h3><p>创建一个切片，其元素是每个map的value调用mapper函数的结果。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> ValuesBy[K comparable, V any, T any](m </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V, mapper </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(item V) T) []T</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> ValuesBy</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">,</span><span style="color:#393A34;"> T any</span><span style="color:#999999;">](</span><span style="color:#393A34;">m </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">,</span><span style="color:#393A34;"> mapper </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">item V</span><span style="color:#999999;">)</span><span style="color:#393A34;"> T</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#393A34;">T</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/sg9-oRidh8f" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">sort</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    values </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">ValuesBy</span><span style="color:#F6F6F4;">(m, </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(v </span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">) </span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">switch</span><span style="color:#F6F6F4;"> v {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">case</span><span style="color:#F6F6F4;"> </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">:</span></span>
<span class="line"><span style="color:#F6F6F4;">            </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a-1</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">case</span><span style="color:#F6F6F4;"> </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">:</span></span>
<span class="line"><span style="color:#F6F6F4;">            </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b-2</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">case</span><span style="color:#F6F6F4;"> </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">:</span></span>
<span class="line"><span style="color:#F6F6F4;">            </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c-3</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">default</span><span style="color:#F6F6F4;">:</span></span>
<span class="line"><span style="color:#F6F6F4;">            </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#DEE492;">&quot;&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">        }</span></span>
<span class="line"><span style="color:#F6F6F4;">    })</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    sort.</span><span style="color:#97E1F1;">Strings</span><span style="color:#F6F6F4;">(values)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(values)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// [a-1 b-2 c-3]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">sort</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">]</span><span style="color:#AB5959;">string</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">1</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">2</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">3</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">values</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">ValuesBy</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">v </span><span style="color:#AB5959;">string</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">string</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">switch</span><span style="color:#393A34;"> v </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">case</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span></span>
<span class="line"><span style="color:#393A34;">            </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a-1</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">case</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span></span>
<span class="line"><span style="color:#393A34;">            </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b-2</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">case</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span></span>
<span class="line"><span style="color:#393A34;">            </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c-3</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">default</span><span style="color:#999999;">:</span></span>
<span class="line"><span style="color:#393A34;">            </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;&quot;</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    sort</span><span style="color:#999999;">.</span><span style="color:#998418;">Strings</span><span style="color:#999999;">(</span><span style="color:#393A34;">values</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">values</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// [a-1 b-2 c-3]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="MapKeys" tabindex="-1"><span id="MapKeys">MapKeys</span> <a class="header-anchor" href="#MapKeys" aria-label="Permalink to &quot;&lt;span id=&quot;MapKeys&quot;&gt;MapKeys&lt;/span&gt;&quot;">​</a></h3><p>操作map的每个key，然后转为新的map。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> MapKeys[K comparable, V any, T comparable](m </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V, iteratee </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(key K, value V) T) </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[T]V</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> MapKeys</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">,</span><span style="color:#393A34;"> T comparable</span><span style="color:#999999;">](</span><span style="color:#393A34;">m </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">,</span><span style="color:#393A34;"> iteratee </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">key K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> value V</span><span style="color:#999999;">)</span><span style="color:#393A34;"> T</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">T</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/8scDxWeBDKd" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">strconv</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">MapKeys</span><span style="color:#F6F6F4;">(m, </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(k </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">, _ </span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">) </span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> strconv.</span><span style="color:#97E1F1;">Itoa</span><span style="color:#F6F6F4;">(k)</span></span>
<span class="line"><span style="color:#F6F6F4;">    })</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// map[1:a 2:b 3:c]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">strconv</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">]</span><span style="color:#AB5959;">string</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">1</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">2</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">3</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">MapKeys</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">k </span><span style="color:#AB5959;">int</span><span style="color:#999999;">,</span><span style="color:#393A34;"> _ </span><span style="color:#AB5959;">string</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">string</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> strconv</span><span style="color:#999999;">.</span><span style="color:#998418;">Itoa</span><span style="color:#999999;">(</span><span style="color:#393A34;">k</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// map[1:a 2:b 3:c]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="MapValues" tabindex="-1"><span id="MapValues">MapValues</span> <a class="header-anchor" href="#MapValues" aria-label="Permalink to &quot;&lt;span id=&quot;MapValues&quot;&gt;MapValues&lt;/span&gt;&quot;">​</a></h3><p>操作map的每个value，然后转为新的map。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> MapValues[K comparable, V any, T any](m </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V, iteratee </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(key K, value V) T) </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]T</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> MapValues</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">,</span><span style="color:#393A34;"> T any</span><span style="color:#999999;">](</span><span style="color:#393A34;">m </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">,</span><span style="color:#393A34;"> iteratee </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">key K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> value V</span><span style="color:#999999;">)</span><span style="color:#393A34;"> T</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">T</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/g92aY3fc7Iw" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">strconv</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">MapValues</span><span style="color:#F6F6F4;">(m, </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(k </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">, v </span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">) </span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> v </span><span style="color:#F286C4;">+</span><span style="color:#F6F6F4;"> strconv.</span><span style="color:#97E1F1;">Itoa</span><span style="color:#F6F6F4;">(k)</span></span>
<span class="line"><span style="color:#F6F6F4;">    })</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// map[1:a1 2:b2 3:c3]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">strconv</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">int</span><span style="color:#999999;">]</span><span style="color:#AB5959;">string</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">1</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">2</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#2F798A;">3</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">MapValues</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">k </span><span style="color:#AB5959;">int</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v </span><span style="color:#AB5959;">string</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">string</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> v </span><span style="color:#AB5959;">+</span><span style="color:#393A34;"> strconv</span><span style="color:#999999;">.</span><span style="color:#998418;">Itoa</span><span style="color:#999999;">(</span><span style="color:#393A34;">k</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// map[1:a1 2:b2 3:c3]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Entry" tabindex="-1"><span id="Entry">Entry</span> <a class="header-anchor" href="#Entry" aria-label="Permalink to &quot;&lt;span id=&quot;Entry&quot;&gt;Entry&lt;/span&gt;&quot;">​</a></h3><p>将map转换为键/值对切片。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">Entry</span><span style="color:#F6F6F4;">[K comparable, V any] </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">    Key   K</span></span>
<span class="line"><span style="color:#F6F6F4;">    Value V</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> Entries[K comparable, V any](m </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V) []Entry[K, V]</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">Entry</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">]</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    Key   K</span></span>
<span class="line"><span style="color:#393A34;">    Value V</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> Entries</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">](</span><span style="color:#393A34;">m </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">[]</span><span style="color:#393A34;">Entry</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V</span><span style="color:#999999;">]</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/Ltb11LNcElY" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">sort</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">Entries</span><span style="color:#F6F6F4;">(m)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    sort.</span><span style="color:#97E1F1;">Slice</span><span style="color:#F6F6F4;">(result, </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(i, j </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) </span><span style="color:#97E1F1;font-style:italic;">bool</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> result[i].Value </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> result[j].Value</span></span>
<span class="line"><span style="color:#F6F6F4;">    })</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// [{a 1} {b 2} {c 3}]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">sort</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">Entries</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    sort</span><span style="color:#999999;">.</span><span style="color:#998418;">Slice</span><span style="color:#999999;">(</span><span style="color:#393A34;">result</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">i</span><span style="color:#999999;">,</span><span style="color:#393A34;"> j </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">bool</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> result</span><span style="color:#999999;">[</span><span style="color:#393A34;">i</span><span style="color:#999999;">].</span><span style="color:#393A34;">Value </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> result</span><span style="color:#999999;">[</span><span style="color:#393A34;">j</span><span style="color:#999999;">].</span><span style="color:#393A34;">Value</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// [{a 1} {b 2} {c 3}]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="FromEntries" tabindex="-1"><span id="FromEntries">FromEntries</span> <a class="header-anchor" href="#FromEntries" aria-label="Permalink to &quot;&lt;span id=&quot;FromEntries&quot;&gt;FromEntries&lt;/span&gt;&quot;">​</a></h3><p>基于键/值对的切片创建map。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">type</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;font-style:italic;">Entry</span><span style="color:#F6F6F4;">[K comparable, V any] </span><span style="color:#F286C4;">struct</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">    Key   K</span></span>
<span class="line"><span style="color:#F6F6F4;">    Value V</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> FromEntries[K comparable, V any](entries []Entry[K, V]) </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">type</span><span style="color:#393A34;"> </span><span style="color:#2E8F82;">Entry</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">]</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">struct</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    Key   K</span></span>
<span class="line"><span style="color:#393A34;">    Value V</span></span>
<span class="line"><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> FromEntries</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">](</span><span style="color:#393A34;">entries </span><span style="color:#999999;">[]</span><span style="color:#393A34;">Entry</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/C8L4ul9TVwf" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    result </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">FromEntries</span><span style="color:#F6F6F4;">([]Entry[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">, </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">]{</span></span>
<span class="line"><span style="color:#F6F6F4;">        {Key: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, Value: </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">},</span></span>
<span class="line"><span style="color:#F6F6F4;">        {Key: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, Value: </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">},</span></span>
<span class="line"><span style="color:#F6F6F4;">        {Key: </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, Value: </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">},</span></span>
<span class="line"><span style="color:#F6F6F4;">    })</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// map[a:1 b:2 c:3]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">FromEntries</span><span style="color:#999999;">([]</span><span style="color:#393A34;">Entry</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#999999;">]{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">{</span><span style="color:#393A34;">Key</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> Value</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">},</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">{</span><span style="color:#393A34;">Key</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> Value</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">},</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">{</span><span style="color:#393A34;">Key</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> Value</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">},</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// map[a:1 b:2 c:3]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="Transform" tabindex="-1"><span id="Transform">Transform</span> <a class="header-anchor" href="#Transform" aria-label="Permalink to &quot;&lt;span id=&quot;Transform&quot;&gt;Transform&lt;/span&gt;&quot;">​</a></h3><p>将map转换为其他类型的map。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> Transform[K1 comparable, V1 any, K2 comparable, V2 any](m </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K1]V1, iteratee </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(key K1, value V1) (K2, V2)) </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K2]V2</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> Transform</span><span style="color:#999999;">[</span><span style="color:#393A34;">K1 comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V1 any</span><span style="color:#999999;">,</span><span style="color:#393A34;"> K2 comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V2 any</span><span style="color:#999999;">](</span><span style="color:#393A34;">m </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K1</span><span style="color:#999999;">]</span><span style="color:#393A34;">V1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> iteratee </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">key K1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> value V1</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">K2</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V2</span><span style="color:#999999;">))</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K2</span><span style="color:#999999;">]</span><span style="color:#393A34;">V2</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/P6ovfToM3zj" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">strconv</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#97E1F1;">Transform</span><span style="color:#F6F6F4;">(m, </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(k </span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">, v </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) (</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">, </span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">) {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> k, strconv.</span><span style="color:#97E1F1;">Itoa</span><span style="color:#F6F6F4;">(v)</span></span>
<span class="line"><span style="color:#F6F6F4;">    })</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// map[a:1 b:2 c:3]</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">strconv</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#998418;">Transform</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">k </span><span style="color:#AB5959;">string</span><span style="color:#999999;">,</span><span style="color:#393A34;"> v </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#AB5959;">string</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">string</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> k</span><span style="color:#999999;">,</span><span style="color:#393A34;"> strconv</span><span style="color:#999999;">.</span><span style="color:#998418;">Itoa</span><span style="color:#999999;">(</span><span style="color:#393A34;">v</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">})</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// map[a:1 b:2 c:3]</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="IsDisjoint" tabindex="-1"><span id="IsDisjoint">IsDisjoint</span> <a class="header-anchor" href="#IsDisjoint" aria-label="Permalink to &quot;&lt;span id=&quot;IsDisjoint&quot;&gt;IsDisjoint&lt;/span&gt;&quot;">​</a></h3><p>验证两个map是否具有不同的key</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> IsDisjoint[K comparable, V any](mapA, mapB </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V) </span><span style="color:#97E1F1;font-style:italic;">bool</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> IsDisjoint</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">](</span><span style="color:#393A34;">mapA</span><span style="color:#999999;">,</span><span style="color:#393A34;"> mapB </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">bool</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/N9qgYg_Ho6f" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m1 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">3</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    m2 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">22</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    m3 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">22</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result1 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">IsDisjoint</span><span style="color:#F6F6F4;">(m1, m2)</span></span>
<span class="line"><span style="color:#F6F6F4;">    result2 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">IsDisjoint</span><span style="color:#F6F6F4;">(m1, m3)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result1)</span></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result2)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// true</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// false</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m1</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">3</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m2</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">22</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m3</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">22</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result1</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">IsDisjoint</span><span style="color:#999999;">(</span><span style="color:#393A34;">m1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> m2</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result2</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">IsDisjoint</span><span style="color:#999999;">(</span><span style="color:#393A34;">m1</span><span style="color:#999999;">,</span><span style="color:#393A34;"> m3</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result1</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result2</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// true</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// false</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="HasKey" tabindex="-1"><span id="HasKey">HasKey</span> <a class="header-anchor" href="#HasKey" aria-label="Permalink to &quot;&lt;span id=&quot;HasKey&quot;&gt;HasKey&lt;/span&gt;&quot;">​</a></h3><p>检查map是否包含某个key。用于代替以下样板代码:</p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F6F6F4;">_, haskey </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> amap[</span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">baz</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">];</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">if</span><span style="color:#F6F6F4;"> haskey {</span></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(</span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">map has key baz</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#B07D48;">_</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">haskey</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> amap</span><span style="color:#999999;">[</span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">baz</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">];</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">if</span><span style="color:#393A34;"> haskey </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">map has key baz</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> HasKey[K comparable, V any](m </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[K]V, key K) </span><span style="color:#97E1F1;font-style:italic;">bool</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> HasKey</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">](</span><span style="color:#393A34;">m </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">]</span><span style="color:#393A34;">V</span><span style="color:#999999;">,</span><span style="color:#393A34;"> key K</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">bool</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/isZZHOsDhFc" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    m </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">map</span><span style="color:#F6F6F4;">[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">]</span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">{</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">1</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">b</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">: </span><span style="color:#BF9EEE;">2</span><span style="color:#F6F6F4;">,</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    result1 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">HasKey</span><span style="color:#F6F6F4;">(m, </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">a</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    result2 </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.</span><span style="color:#97E1F1;">HasKey</span><span style="color:#F6F6F4;">(m, </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">c</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result1)</span></span>
<span class="line"><span style="color:#F6F6F4;">    fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(result2)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// Output:</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// true</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// false</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">m</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">map</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">]</span><span style="color:#AB5959;">int</span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">1</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">b</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">:</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">2</span><span style="color:#999999;">,</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result1</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">HasKey</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">a</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">result2</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#998418;">HasKey</span><span style="color:#999999;">(</span><span style="color:#393A34;">m</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B5695999;">&quot;</span><span style="color:#B56959;">c</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result1</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">result2</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// Output:</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// true</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// false</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="NewConcurrentMap" tabindex="-1"><span id="NewConcurrentMap">NewConcurrentMap</span> <a class="header-anchor" href="#NewConcurrentMap" aria-label="Permalink to &quot;&lt;span id=&quot;NewConcurrentMap&quot;&gt;NewConcurrentMap&lt;/span&gt;&quot;">​</a></h3><p>ConcurrentMap协程安全的map结构。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#7B7F8B;">// NewConcurrentMap create a ConcurrentMap with specific shard count.</span></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> NewConcurrentMap[K comparable, V any](shardCount </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">ConcurrentMap[K, V]</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#A0ADA0;">// NewConcurrentMap create a ConcurrentMap with specific shard count.</span></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> NewConcurrentMap</span><span style="color:#999999;">[</span><span style="color:#393A34;">K comparable</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V any</span><span style="color:#999999;">](</span><span style="color:#393A34;">shardCount </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">ConcurrentMap</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V</span><span style="color:#999999;">]</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/3PenTPETJT0" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// create a ConcurrentMap whose key type is string, value type is int</span></span>
<span class="line"><span style="color:#F6F6F4;">    cm </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.NewConcurrentMap[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">, </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">](</span><span style="color:#BF9EEE;">100</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// create a ConcurrentMap whose key type is string, value type is int</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">cm</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewConcurrentMap</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#999999;">](</span><span style="color:#2F798A;">100</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="ConcurrentMap-Set" tabindex="-1"><span id="ConcurrentMap_Set">ConcurrentMap_Set</span> <a class="header-anchor" href="#ConcurrentMap-Set" aria-label="Permalink to &quot;&lt;span id=&quot;ConcurrentMap_Set&quot;&gt;ConcurrentMap_Set&lt;/span&gt;&quot;">​</a></h3><p>在map中设置key和value。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (cm </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">ConcurrentMap[K, V]) </span><span style="color:#62E884;">Set</span><span style="color:#F6F6F4;">(key K, value V)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">cm </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">ConcurrentMap</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Set</span><span style="color:#999999;">(</span><span style="color:#393A34;">key K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> value V</span><span style="color:#999999;">)</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/3PenTPETJT0" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    cm </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.NewConcurrentMap[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">, </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">](</span><span style="color:#BF9EEE;">100</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">var</span><span style="color:#F6F6F4;"> wg1 sync.WaitGroup</span></span>
<span class="line"><span style="color:#F6F6F4;">    wg1.</span><span style="color:#97E1F1;">Add</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> i </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span><span style="color:#F6F6F4;">; i </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">; i</span><span style="color:#F286C4;">++</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">go</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(n </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) {</span></span>
<span class="line"><span style="color:#F6F6F4;">            cm.</span><span style="color:#97E1F1;">Set</span><span style="color:#F6F6F4;">(fmt.</span><span style="color:#97E1F1;">Sprintf</span><span style="color:#F6F6F4;">(</span><span style="color:#DEE492;">&quot;</span><span style="color:#BF9EEE;">%d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, n), n)</span></span>
<span class="line"><span style="color:#F6F6F4;">            wg1.</span><span style="color:#97E1F1;">Done</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"><span style="color:#F6F6F4;">        }(i)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    wg1.</span><span style="color:#97E1F1;">Wait</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">var</span><span style="color:#F6F6F4;"> wg2 sync.WaitGroup</span></span>
<span class="line"><span style="color:#F6F6F4;">	wg2.</span><span style="color:#97E1F1;">Add</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> j </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span><span style="color:#F6F6F4;">; j </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">; j</span><span style="color:#F286C4;">++</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">go</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(n </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) {</span></span>
<span class="line"><span style="color:#F6F6F4;">            val, ok </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> cm.</span><span style="color:#97E1F1;">Get</span><span style="color:#F6F6F4;">(fmt.</span><span style="color:#97E1F1;">Sprintf</span><span style="color:#F6F6F4;">(</span><span style="color:#DEE492;">&quot;</span><span style="color:#BF9EEE;">%d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, n))</span></span>
<span class="line"><span style="color:#F6F6F4;">            fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(val, ok)</span></span>
<span class="line"><span style="color:#F6F6F4;">			wg2.</span><span style="color:#97E1F1;">Done</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"><span style="color:#F6F6F4;">        }(j)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">	wg2.</span><span style="color:#97E1F1;">Wait</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// output: (order may change)</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 1 true</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 3 true</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 2 true</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 0 true</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 4 true</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">cm</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewConcurrentMap</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#999999;">](</span><span style="color:#2F798A;">100</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">var</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">wg1</span><span style="color:#393A34;"> sync</span><span style="color:#999999;">.</span><span style="color:#393A34;">WaitGroup</span></span>
<span class="line"><span style="color:#393A34;">    wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Add</span><span style="color:#999999;">(</span><span style="color:#2F798A;">5</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">i</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i</span><span style="color:#AB5959;">++</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">go</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">n </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">            cm</span><span style="color:#999999;">.</span><span style="color:#998418;">Set</span><span style="color:#999999;">(</span><span style="color:#393A34;">fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Sprintf</span><span style="color:#999999;">(</span><span style="color:#B5695999;">&quot;</span><span style="color:#A65E2B;">%d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> n</span><span style="color:#999999;">),</span><span style="color:#393A34;"> n</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">            wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Done</span><span style="color:#999999;">()</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">}(</span><span style="color:#393A34;">i</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Wait</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">var</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">wg2</span><span style="color:#393A34;"> sync</span><span style="color:#999999;">.</span><span style="color:#393A34;">WaitGroup</span></span>
<span class="line"><span style="color:#393A34;">	wg2</span><span style="color:#999999;">.</span><span style="color:#998418;">Add</span><span style="color:#999999;">(</span><span style="color:#2F798A;">5</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">j</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span><span style="color:#999999;">;</span><span style="color:#393A34;"> j </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">;</span><span style="color:#393A34;"> j</span><span style="color:#AB5959;">++</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">go</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">n </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">            </span><span style="color:#B07D48;">val</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">ok</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> cm</span><span style="color:#999999;">.</span><span style="color:#998418;">Get</span><span style="color:#999999;">(</span><span style="color:#393A34;">fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Sprintf</span><span style="color:#999999;">(</span><span style="color:#B5695999;">&quot;</span><span style="color:#A65E2B;">%d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> n</span><span style="color:#999999;">))</span></span>
<span class="line"><span style="color:#393A34;">            fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">val</span><span style="color:#999999;">,</span><span style="color:#393A34;"> ok</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">			wg2</span><span style="color:#999999;">.</span><span style="color:#998418;">Done</span><span style="color:#999999;">()</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">}(</span><span style="color:#393A34;">j</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">	wg2</span><span style="color:#999999;">.</span><span style="color:#998418;">Wait</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// output: (order may change)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 1 true</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 3 true</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 2 true</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 0 true</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 4 true</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="ConcurrentMap-Get" tabindex="-1"><span id="ConcurrentMap_Get">ConcurrentMap_Get</span> <a class="header-anchor" href="#ConcurrentMap-Get" aria-label="Permalink to &quot;&lt;span id=&quot;ConcurrentMap_Get&quot;&gt;ConcurrentMap_Get&lt;/span&gt;&quot;">​</a></h3><p>根据key获取value, 如果不存在key,返回零值。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (cm </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">ConcurrentMap[K, V]) </span><span style="color:#62E884;">Get</span><span style="color:#F6F6F4;">(key K) (V, </span><span style="color:#97E1F1;font-style:italic;">bool</span><span style="color:#F6F6F4;">)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">cm </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">ConcurrentMap</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Get</span><span style="color:#999999;">(</span><span style="color:#393A34;">key K</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">V</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">bool</span><span style="color:#999999;">)</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/3PenTPETJT0" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    cm </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.NewConcurrentMap[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">, </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">](</span><span style="color:#BF9EEE;">100</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">var</span><span style="color:#F6F6F4;"> wg1 sync.WaitGroup</span></span>
<span class="line"><span style="color:#F6F6F4;">    wg1.</span><span style="color:#97E1F1;">Add</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> i </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span><span style="color:#F6F6F4;">; i </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">; i</span><span style="color:#F286C4;">++</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">go</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(n </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) {</span></span>
<span class="line"><span style="color:#F6F6F4;">            cm.</span><span style="color:#97E1F1;">Set</span><span style="color:#F6F6F4;">(fmt.</span><span style="color:#97E1F1;">Sprintf</span><span style="color:#F6F6F4;">(</span><span style="color:#DEE492;">&quot;</span><span style="color:#BF9EEE;">%d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, n), n)</span></span>
<span class="line"><span style="color:#F6F6F4;">            wg1.</span><span style="color:#97E1F1;">Done</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"><span style="color:#F6F6F4;">        }(i)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    wg1.</span><span style="color:#97E1F1;">Wait</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">var</span><span style="color:#F6F6F4;"> wg2 sync.WaitGroup</span></span>
<span class="line"><span style="color:#F6F6F4;">	wg2.</span><span style="color:#97E1F1;">Add</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> j </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span><span style="color:#F6F6F4;">; j </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">; j</span><span style="color:#F286C4;">++</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">go</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(n </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) {</span></span>
<span class="line"><span style="color:#F6F6F4;">            val, ok </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> cm.</span><span style="color:#97E1F1;">Get</span><span style="color:#F6F6F4;">(fmt.</span><span style="color:#97E1F1;">Sprintf</span><span style="color:#F6F6F4;">(</span><span style="color:#DEE492;">&quot;</span><span style="color:#BF9EEE;">%d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, n))</span></span>
<span class="line"><span style="color:#F6F6F4;">            fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(val, ok)</span></span>
<span class="line"><span style="color:#F6F6F4;">			wg2.</span><span style="color:#97E1F1;">Done</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"><span style="color:#F6F6F4;">        }(j)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">	wg2.</span><span style="color:#97E1F1;">Wait</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// output: (order may change)</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 1 true</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 3 true</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 2 true</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 0 true</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 4 true</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">cm</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewConcurrentMap</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#999999;">](</span><span style="color:#2F798A;">100</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">var</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">wg1</span><span style="color:#393A34;"> sync</span><span style="color:#999999;">.</span><span style="color:#393A34;">WaitGroup</span></span>
<span class="line"><span style="color:#393A34;">    wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Add</span><span style="color:#999999;">(</span><span style="color:#2F798A;">5</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">i</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i</span><span style="color:#AB5959;">++</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">go</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">n </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">            cm</span><span style="color:#999999;">.</span><span style="color:#998418;">Set</span><span style="color:#999999;">(</span><span style="color:#393A34;">fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Sprintf</span><span style="color:#999999;">(</span><span style="color:#B5695999;">&quot;</span><span style="color:#A65E2B;">%d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> n</span><span style="color:#999999;">),</span><span style="color:#393A34;"> n</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">            wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Done</span><span style="color:#999999;">()</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">}(</span><span style="color:#393A34;">i</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Wait</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">var</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">wg2</span><span style="color:#393A34;"> sync</span><span style="color:#999999;">.</span><span style="color:#393A34;">WaitGroup</span></span>
<span class="line"><span style="color:#393A34;">	wg2</span><span style="color:#999999;">.</span><span style="color:#998418;">Add</span><span style="color:#999999;">(</span><span style="color:#2F798A;">5</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">j</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span><span style="color:#999999;">;</span><span style="color:#393A34;"> j </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">;</span><span style="color:#393A34;"> j</span><span style="color:#AB5959;">++</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">go</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">n </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">            </span><span style="color:#B07D48;">val</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">ok</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> cm</span><span style="color:#999999;">.</span><span style="color:#998418;">Get</span><span style="color:#999999;">(</span><span style="color:#393A34;">fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Sprintf</span><span style="color:#999999;">(</span><span style="color:#B5695999;">&quot;</span><span style="color:#A65E2B;">%d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> n</span><span style="color:#999999;">))</span></span>
<span class="line"><span style="color:#393A34;">            fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">val</span><span style="color:#999999;">,</span><span style="color:#393A34;"> ok</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">			wg2</span><span style="color:#999999;">.</span><span style="color:#998418;">Done</span><span style="color:#999999;">()</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">}(</span><span style="color:#393A34;">j</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">	wg2</span><span style="color:#999999;">.</span><span style="color:#998418;">Wait</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// output: (order may change)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 1 true</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 3 true</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 2 true</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 0 true</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 4 true</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="ConcurrentMap-GetOrSet" tabindex="-1"><span id="ConcurrentMap_GetOrSet">ConcurrentMap_GetOrSet</span> <a class="header-anchor" href="#ConcurrentMap-GetOrSet" aria-label="Permalink to &quot;&lt;span id=&quot;ConcurrentMap_GetOrSet&quot;&gt;ConcurrentMap_GetOrSet&lt;/span&gt;&quot;">​</a></h3><p>返回键的现有值（如果存在），否则，设置key并返回给定值。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (cm </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">ConcurrentMap[K, V]) </span><span style="color:#62E884;">GetOrSet</span><span style="color:#F6F6F4;">(key K, value V) (actual V, ok </span><span style="color:#97E1F1;font-style:italic;">bool</span><span style="color:#F6F6F4;">)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">cm </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">ConcurrentMap</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">GetOrSet</span><span style="color:#999999;">(</span><span style="color:#393A34;">key K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> value V</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">actual V</span><span style="color:#999999;">,</span><span style="color:#393A34;"> ok </span><span style="color:#AB5959;">bool</span><span style="color:#999999;">)</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/aDcDApOK01a" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    cm </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.NewConcurrentMap[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">, </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">](</span><span style="color:#BF9EEE;">100</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">var</span><span style="color:#F6F6F4;"> wg sync.WaitGroup</span></span>
<span class="line"><span style="color:#F6F6F4;">    wg.</span><span style="color:#97E1F1;">Add</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> i </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span><span style="color:#F6F6F4;">; i </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">; i</span><span style="color:#F286C4;">++</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">go</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(n </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) {</span></span>
<span class="line"><span style="color:#F6F6F4;">            val, ok </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> cm.</span><span style="color:#97E1F1;">GetOrSet</span><span style="color:#F6F6F4;">(fmt.</span><span style="color:#97E1F1;">Sprintf</span><span style="color:#F6F6F4;">(</span><span style="color:#DEE492;">&quot;</span><span style="color:#BF9EEE;">%d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, n), n)</span></span>
<span class="line"><span style="color:#F6F6F4;">            fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(val, ok)</span></span>
<span class="line"><span style="color:#F6F6F4;">            wg.</span><span style="color:#97E1F1;">Done</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"><span style="color:#F6F6F4;">        }(i)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    wg.</span><span style="color:#97E1F1;">Wait</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// output: (order may change)</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 1 false</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 3 false</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 2 false</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 0 false</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#7B7F8B;">// 4 false</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">cm</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewConcurrentMap</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#999999;">](</span><span style="color:#2F798A;">100</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">var</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">wg</span><span style="color:#393A34;"> sync</span><span style="color:#999999;">.</span><span style="color:#393A34;">WaitGroup</span></span>
<span class="line"><span style="color:#393A34;">    wg</span><span style="color:#999999;">.</span><span style="color:#998418;">Add</span><span style="color:#999999;">(</span><span style="color:#2F798A;">5</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">i</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i</span><span style="color:#AB5959;">++</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">go</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">n </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">            </span><span style="color:#B07D48;">val</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">ok</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> cm</span><span style="color:#999999;">.</span><span style="color:#998418;">GetOrSet</span><span style="color:#999999;">(</span><span style="color:#393A34;">fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Sprintf</span><span style="color:#999999;">(</span><span style="color:#B5695999;">&quot;</span><span style="color:#A65E2B;">%d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> n</span><span style="color:#999999;">),</span><span style="color:#393A34;"> n</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">            fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">val</span><span style="color:#999999;">,</span><span style="color:#393A34;"> ok</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">            wg</span><span style="color:#999999;">.</span><span style="color:#998418;">Done</span><span style="color:#999999;">()</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">}(</span><span style="color:#393A34;">i</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    wg</span><span style="color:#999999;">.</span><span style="color:#998418;">Wait</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// output: (order may change)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 1 false</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 3 false</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 2 false</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 0 false</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#A0ADA0;">// 4 false</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="ConcurrentMap-Delete" tabindex="-1"><span id="ConcurrentMap_Delete">ConcurrentMap_Delete</span> <a class="header-anchor" href="#ConcurrentMap-Delete" aria-label="Permalink to &quot;&lt;span id=&quot;ConcurrentMap_Delete&quot;&gt;ConcurrentMap_Delete&lt;/span&gt;&quot;">​</a></h3><p>删除key。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (cm </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">ConcurrentMap[K, V]) </span><span style="color:#62E884;">Delete</span><span style="color:#F6F6F4;">(key K)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">cm </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">ConcurrentMap</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Delete</span><span style="color:#999999;">(</span><span style="color:#393A34;">key K</span><span style="color:#999999;">)</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/uTIJZYhpVMS" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    cm </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.NewConcurrentMap[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">, </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">](</span><span style="color:#BF9EEE;">100</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">var</span><span style="color:#F6F6F4;"> wg1 sync.WaitGroup</span></span>
<span class="line"><span style="color:#F6F6F4;">    wg1.</span><span style="color:#97E1F1;">Add</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> i </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span><span style="color:#F6F6F4;">; i </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">; i</span><span style="color:#F286C4;">++</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">go</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(n </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) {</span></span>
<span class="line"><span style="color:#F6F6F4;">            cm.</span><span style="color:#97E1F1;">Set</span><span style="color:#F6F6F4;">(fmt.</span><span style="color:#97E1F1;">Sprintf</span><span style="color:#F6F6F4;">(</span><span style="color:#DEE492;">&quot;</span><span style="color:#BF9EEE;">%d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, n), n)</span></span>
<span class="line"><span style="color:#F6F6F4;">            wg1.</span><span style="color:#97E1F1;">Done</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"><span style="color:#F6F6F4;">        }(i)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    wg1.</span><span style="color:#97E1F1;">Wait</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">var</span><span style="color:#F6F6F4;"> wg2 sync.WaitGroup</span></span>
<span class="line"><span style="color:#F6F6F4;">	wg2.</span><span style="color:#97E1F1;">Add</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> j </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span><span style="color:#F6F6F4;">; j </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">; j</span><span style="color:#F286C4;">++</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">go</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(n </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) {</span></span>
<span class="line"><span style="color:#F6F6F4;">            cm.</span><span style="color:#97E1F1;">Delete</span><span style="color:#F6F6F4;">(fmt.</span><span style="color:#97E1F1;">Sprintf</span><span style="color:#F6F6F4;">(</span><span style="color:#DEE492;">&quot;</span><span style="color:#BF9EEE;">%d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, n))</span></span>
<span class="line"><span style="color:#F6F6F4;">            wg2.</span><span style="color:#97E1F1;">Done</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"><span style="color:#F6F6F4;">        }(i)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    wg2.</span><span style="color:#97E1F1;">Wait</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">cm</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewConcurrentMap</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#999999;">](</span><span style="color:#2F798A;">100</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">var</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">wg1</span><span style="color:#393A34;"> sync</span><span style="color:#999999;">.</span><span style="color:#393A34;">WaitGroup</span></span>
<span class="line"><span style="color:#393A34;">    wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Add</span><span style="color:#999999;">(</span><span style="color:#2F798A;">5</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">i</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i</span><span style="color:#AB5959;">++</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">go</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">n </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">            cm</span><span style="color:#999999;">.</span><span style="color:#998418;">Set</span><span style="color:#999999;">(</span><span style="color:#393A34;">fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Sprintf</span><span style="color:#999999;">(</span><span style="color:#B5695999;">&quot;</span><span style="color:#A65E2B;">%d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> n</span><span style="color:#999999;">),</span><span style="color:#393A34;"> n</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">            wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Done</span><span style="color:#999999;">()</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">}(</span><span style="color:#393A34;">i</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Wait</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">var</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">wg2</span><span style="color:#393A34;"> sync</span><span style="color:#999999;">.</span><span style="color:#393A34;">WaitGroup</span></span>
<span class="line"><span style="color:#393A34;">	wg2</span><span style="color:#999999;">.</span><span style="color:#998418;">Add</span><span style="color:#999999;">(</span><span style="color:#2F798A;">5</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">j</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span><span style="color:#999999;">;</span><span style="color:#393A34;"> j </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">;</span><span style="color:#393A34;"> j</span><span style="color:#AB5959;">++</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">go</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">n </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">            cm</span><span style="color:#999999;">.</span><span style="color:#998418;">Delete</span><span style="color:#999999;">(</span><span style="color:#393A34;">fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Sprintf</span><span style="color:#999999;">(</span><span style="color:#B5695999;">&quot;</span><span style="color:#A65E2B;">%d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> n</span><span style="color:#999999;">))</span></span>
<span class="line"><span style="color:#393A34;">            wg2</span><span style="color:#999999;">.</span><span style="color:#998418;">Done</span><span style="color:#999999;">()</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">}(</span><span style="color:#393A34;">i</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    wg2</span><span style="color:#999999;">.</span><span style="color:#998418;">Wait</span><span style="color:#999999;">()</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="ConcurrentMap-GetAndDelete" tabindex="-1"><span id="ConcurrentMap_GetAndDelete">ConcurrentMap_GetAndDelete</span> <a class="header-anchor" href="#ConcurrentMap-GetAndDelete" aria-label="Permalink to &quot;&lt;span id=&quot;ConcurrentMap_GetAndDelete&quot;&gt;ConcurrentMap_GetAndDelete&lt;/span&gt;&quot;">​</a></h3><p>获取key，然后删除。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (cm </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">ConcurrentMap[K, V]) </span><span style="color:#62E884;">GetAndDelete</span><span style="color:#F6F6F4;">(key K) (actual V, ok </span><span style="color:#97E1F1;font-style:italic;">bool</span><span style="color:#F6F6F4;">)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">cm </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">ConcurrentMap</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">GetAndDelete</span><span style="color:#999999;">(</span><span style="color:#393A34;">key K</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">actual V</span><span style="color:#999999;">,</span><span style="color:#393A34;"> ok </span><span style="color:#AB5959;">bool</span><span style="color:#999999;">)</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/ZyxeIXSZUiM" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    cm </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.NewConcurrentMap[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">, </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">](</span><span style="color:#BF9EEE;">100</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">var</span><span style="color:#F6F6F4;"> wg1 sync.WaitGroup</span></span>
<span class="line"><span style="color:#F6F6F4;">    wg1.</span><span style="color:#97E1F1;">Add</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> i </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span><span style="color:#F6F6F4;">; i </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">; i</span><span style="color:#F286C4;">++</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">go</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(n </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) {</span></span>
<span class="line"><span style="color:#F6F6F4;">            cm.</span><span style="color:#97E1F1;">Set</span><span style="color:#F6F6F4;">(fmt.</span><span style="color:#97E1F1;">Sprintf</span><span style="color:#F6F6F4;">(</span><span style="color:#DEE492;">&quot;</span><span style="color:#BF9EEE;">%d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, n), n)</span></span>
<span class="line"><span style="color:#F6F6F4;">            wg1.</span><span style="color:#97E1F1;">Done</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"><span style="color:#F6F6F4;">        }(i)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    wg1.</span><span style="color:#97E1F1;">Wait</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">var</span><span style="color:#F6F6F4;"> wg2 sync.WaitGroup</span></span>
<span class="line"><span style="color:#F6F6F4;">	wg2.</span><span style="color:#97E1F1;">Add</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> j </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span><span style="color:#F6F6F4;">; j </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">; j</span><span style="color:#F286C4;">++</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">go</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(n </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) {</span></span>
<span class="line"><span style="color:#F6F6F4;">            val, ok </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> cm.</span><span style="color:#97E1F1;">GetAndDelete</span><span style="color:#F6F6F4;">(fmt.</span><span style="color:#97E1F1;">Sprintf</span><span style="color:#F6F6F4;">(</span><span style="color:#DEE492;">&quot;</span><span style="color:#BF9EEE;">%d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, n))</span></span>
<span class="line"><span style="color:#F6F6F4;">            fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(val, ok) </span><span style="color:#7B7F8B;">//n, true</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">            _, ok </span><span style="color:#F286C4;">=</span><span style="color:#F6F6F4;"> cm.</span><span style="color:#97E1F1;">Get</span><span style="color:#F6F6F4;">(fmt.</span><span style="color:#97E1F1;">Sprintf</span><span style="color:#F6F6F4;">(</span><span style="color:#DEE492;">&quot;</span><span style="color:#BF9EEE;">%d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, n))</span></span>
<span class="line"><span style="color:#F6F6F4;">            fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(val, ok) </span><span style="color:#7B7F8B;">//false</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">            wg2.</span><span style="color:#97E1F1;">Done</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"><span style="color:#F6F6F4;">        }(j)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    wg2.</span><span style="color:#97E1F1;">Wait</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">cm</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewConcurrentMap</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#999999;">](</span><span style="color:#2F798A;">100</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">var</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">wg1</span><span style="color:#393A34;"> sync</span><span style="color:#999999;">.</span><span style="color:#393A34;">WaitGroup</span></span>
<span class="line"><span style="color:#393A34;">    wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Add</span><span style="color:#999999;">(</span><span style="color:#2F798A;">5</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">i</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i</span><span style="color:#AB5959;">++</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">go</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">n </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">            cm</span><span style="color:#999999;">.</span><span style="color:#998418;">Set</span><span style="color:#999999;">(</span><span style="color:#393A34;">fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Sprintf</span><span style="color:#999999;">(</span><span style="color:#B5695999;">&quot;</span><span style="color:#A65E2B;">%d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> n</span><span style="color:#999999;">),</span><span style="color:#393A34;"> n</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">            wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Done</span><span style="color:#999999;">()</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">}(</span><span style="color:#393A34;">i</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Wait</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">var</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">wg2</span><span style="color:#393A34;"> sync</span><span style="color:#999999;">.</span><span style="color:#393A34;">WaitGroup</span></span>
<span class="line"><span style="color:#393A34;">	wg2</span><span style="color:#999999;">.</span><span style="color:#998418;">Add</span><span style="color:#999999;">(</span><span style="color:#2F798A;">5</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">j</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span><span style="color:#999999;">;</span><span style="color:#393A34;"> j </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">;</span><span style="color:#393A34;"> j</span><span style="color:#AB5959;">++</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">go</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">n </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">            </span><span style="color:#B07D48;">val</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">ok</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> cm</span><span style="color:#999999;">.</span><span style="color:#998418;">GetAndDelete</span><span style="color:#999999;">(</span><span style="color:#393A34;">fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Sprintf</span><span style="color:#999999;">(</span><span style="color:#B5695999;">&quot;</span><span style="color:#A65E2B;">%d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> n</span><span style="color:#999999;">))</span></span>
<span class="line"><span style="color:#393A34;">            fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">val</span><span style="color:#999999;">,</span><span style="color:#393A34;"> ok</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#A0ADA0;">//n, true</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">            </span><span style="color:#B07D48;">_</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">ok</span><span style="color:#393A34;"> </span><span style="color:#999999;">=</span><span style="color:#393A34;"> cm</span><span style="color:#999999;">.</span><span style="color:#998418;">Get</span><span style="color:#999999;">(</span><span style="color:#393A34;">fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Sprintf</span><span style="color:#999999;">(</span><span style="color:#B5695999;">&quot;</span><span style="color:#A65E2B;">%d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> n</span><span style="color:#999999;">))</span></span>
<span class="line"><span style="color:#393A34;">            fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">val</span><span style="color:#999999;">,</span><span style="color:#393A34;"> ok</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#A0ADA0;">//false</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">            wg2</span><span style="color:#999999;">.</span><span style="color:#998418;">Done</span><span style="color:#999999;">()</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">}(</span><span style="color:#393A34;">j</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    wg2</span><span style="color:#999999;">.</span><span style="color:#998418;">Wait</span><span style="color:#999999;">()</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="ConcurrentMap-Has" tabindex="-1"><span id="ConcurrentMap_Has">ConcurrentMap_Has</span> <a class="header-anchor" href="#ConcurrentMap-Has" aria-label="Permalink to &quot;&lt;span id=&quot;ConcurrentMap_Has&quot;&gt;ConcurrentMap_Has&lt;/span&gt;&quot;">​</a></h3><p>验证是否包含key。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (cm </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">ConcurrentMap[K, V]) </span><span style="color:#62E884;">Has</span><span style="color:#F6F6F4;">(key K) </span><span style="color:#97E1F1;font-style:italic;">bool</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">cm </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">ConcurrentMap</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Has</span><span style="color:#999999;">(</span><span style="color:#393A34;">key K</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">bool</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/C8L4ul9TVwf" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    cm </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.NewConcurrentMap[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">, </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">](</span><span style="color:#BF9EEE;">100</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">var</span><span style="color:#F6F6F4;"> wg1 sync.WaitGroup</span></span>
<span class="line"><span style="color:#F6F6F4;">    wg1.</span><span style="color:#97E1F1;">Add</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> i </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span><span style="color:#F6F6F4;">; i </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">; i</span><span style="color:#F286C4;">++</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">go</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(n </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) {</span></span>
<span class="line"><span style="color:#F6F6F4;">            cm.</span><span style="color:#97E1F1;">Set</span><span style="color:#F6F6F4;">(fmt.</span><span style="color:#97E1F1;">Sprintf</span><span style="color:#F6F6F4;">(</span><span style="color:#DEE492;">&quot;</span><span style="color:#BF9EEE;">%d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, n), n)</span></span>
<span class="line"><span style="color:#F6F6F4;">            wg1.</span><span style="color:#97E1F1;">Done</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"><span style="color:#F6F6F4;">        }(i)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    wg1.</span><span style="color:#97E1F1;">Wait</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">var</span><span style="color:#F6F6F4;"> wg2 sync.WaitGroup</span></span>
<span class="line"><span style="color:#F6F6F4;">	wg2.</span><span style="color:#97E1F1;">Add</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> j </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span><span style="color:#F6F6F4;">; j </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">; j</span><span style="color:#F286C4;">++</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">go</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(n </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) {</span></span>
<span class="line"><span style="color:#F6F6F4;">            ok </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> cm.</span><span style="color:#97E1F1;">Has</span><span style="color:#F6F6F4;">(fmt.</span><span style="color:#97E1F1;">Sprintf</span><span style="color:#F6F6F4;">(</span><span style="color:#DEE492;">&quot;</span><span style="color:#BF9EEE;">%d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, n))</span></span>
<span class="line"><span style="color:#F6F6F4;">            fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(ok) </span><span style="color:#7B7F8B;">// true</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">            wg2.</span><span style="color:#97E1F1;">Done</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"><span style="color:#F6F6F4;">        }(j)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    wg2.</span><span style="color:#97E1F1;">Wait</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">cm</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewConcurrentMap</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#999999;">](</span><span style="color:#2F798A;">100</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">var</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">wg1</span><span style="color:#393A34;"> sync</span><span style="color:#999999;">.</span><span style="color:#393A34;">WaitGroup</span></span>
<span class="line"><span style="color:#393A34;">    wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Add</span><span style="color:#999999;">(</span><span style="color:#2F798A;">5</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">i</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i</span><span style="color:#AB5959;">++</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">go</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">n </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">            cm</span><span style="color:#999999;">.</span><span style="color:#998418;">Set</span><span style="color:#999999;">(</span><span style="color:#393A34;">fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Sprintf</span><span style="color:#999999;">(</span><span style="color:#B5695999;">&quot;</span><span style="color:#A65E2B;">%d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> n</span><span style="color:#999999;">),</span><span style="color:#393A34;"> n</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">            wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Done</span><span style="color:#999999;">()</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">}(</span><span style="color:#393A34;">i</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Wait</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">var</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">wg2</span><span style="color:#393A34;"> sync</span><span style="color:#999999;">.</span><span style="color:#393A34;">WaitGroup</span></span>
<span class="line"><span style="color:#393A34;">	wg2</span><span style="color:#999999;">.</span><span style="color:#998418;">Add</span><span style="color:#999999;">(</span><span style="color:#2F798A;">5</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">j</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span><span style="color:#999999;">;</span><span style="color:#393A34;"> j </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">;</span><span style="color:#393A34;"> j</span><span style="color:#AB5959;">++</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">go</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">n </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">            </span><span style="color:#B07D48;">ok</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> cm</span><span style="color:#999999;">.</span><span style="color:#998418;">Has</span><span style="color:#999999;">(</span><span style="color:#393A34;">fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Sprintf</span><span style="color:#999999;">(</span><span style="color:#B5695999;">&quot;</span><span style="color:#A65E2B;">%d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> n</span><span style="color:#999999;">))</span></span>
<span class="line"><span style="color:#393A34;">            fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">ok</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#A0ADA0;">// true</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">            wg2</span><span style="color:#999999;">.</span><span style="color:#998418;">Done</span><span style="color:#999999;">()</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">}(</span><span style="color:#393A34;">j</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    wg2</span><span style="color:#999999;">.</span><span style="color:#998418;">Wait</span><span style="color:#999999;">()</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div><h3 id="ConcurrentMap-Range" tabindex="-1"><span id="ConcurrentMap_Range">ConcurrentMap_Range</span> <a class="header-anchor" href="#ConcurrentMap-Range" aria-label="Permalink to &quot;&lt;span id=&quot;ConcurrentMap_Range&quot;&gt;ConcurrentMap_Range&lt;/span&gt;&quot;">​</a></h3><p>为map中每个键和值顺序调用迭代器。 如果iterator返回false，则停止迭代。</p><p><b>函数签名:</b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> (cm </span><span style="color:#F286C4;">*</span><span style="color:#F6F6F4;">ConcurrentMap[K, V]) </span><span style="color:#62E884;">Range</span><span style="color:#F6F6F4;">(iterator </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(key K, value V) </span><span style="color:#97E1F1;font-style:italic;">bool</span><span style="color:#F6F6F4;">)</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span><span style="color:#393A34;">cm </span><span style="color:#AB5959;">*</span><span style="color:#393A34;">ConcurrentMap</span><span style="color:#999999;">[</span><span style="color:#393A34;">K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> V</span><span style="color:#999999;">])</span><span style="color:#393A34;"> </span><span style="color:#59873A;">Range</span><span style="color:#999999;">(</span><span style="color:#393A34;">iterator </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">key K</span><span style="color:#999999;">,</span><span style="color:#393A34;"> value V</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">bool</span><span style="color:#999999;">)</span></span></code></pre></div><p><b>示例:<span class="run-container"><a href="https://go.dev/play/p/iqcy7P8P0Pr" target="_blank" rel="noreferrer">运行</a></span></b></p><div class="language-go vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">go</span><pre class="shiki dracula-soft vp-code-dark"><code><span class="line"><span style="color:#F286C4;">package</span><span style="color:#F6F6F4;"> main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">import</span><span style="color:#F6F6F4;"> (</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">fmt</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#DEE492;">&quot;</span><span style="color:#E7EE98;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#DEE492;">&quot;</span></span>
<span class="line"><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;"> </span><span style="color:#62E884;">main</span><span style="color:#F6F6F4;">() {</span></span>
<span class="line"><span style="color:#F6F6F4;">    cm </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> maputil.NewConcurrentMap[</span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">, </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">](</span><span style="color:#BF9EEE;">100</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">var</span><span style="color:#F6F6F4;"> wg1 sync.WaitGroup</span></span>
<span class="line"><span style="color:#F6F6F4;">    wg1.</span><span style="color:#97E1F1;">Add</span><span style="color:#F6F6F4;">(</span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    </span><span style="color:#F286C4;">for</span><span style="color:#F6F6F4;"> i </span><span style="color:#F286C4;">:=</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">0</span><span style="color:#F6F6F4;">; i </span><span style="color:#F286C4;">&lt;</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">5</span><span style="color:#F6F6F4;">; i</span><span style="color:#F286C4;">++</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">go</span><span style="color:#F6F6F4;"> </span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(n </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) {</span></span>
<span class="line"><span style="color:#F6F6F4;">            cm.</span><span style="color:#97E1F1;">Set</span><span style="color:#F6F6F4;">(fmt.</span><span style="color:#97E1F1;">Sprintf</span><span style="color:#F6F6F4;">(</span><span style="color:#DEE492;">&quot;</span><span style="color:#BF9EEE;">%d</span><span style="color:#DEE492;">&quot;</span><span style="color:#F6F6F4;">, n), n)</span></span>
<span class="line"><span style="color:#F6F6F4;">            wg1.</span><span style="color:#97E1F1;">Done</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"><span style="color:#F6F6F4;">        }(i)</span></span>
<span class="line"><span style="color:#F6F6F4;">    }</span></span>
<span class="line"><span style="color:#F6F6F4;">    wg1.</span><span style="color:#97E1F1;">Wait</span><span style="color:#F6F6F4;">()</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#F6F6F4;">    cm.</span><span style="color:#97E1F1;">Range</span><span style="color:#F6F6F4;">(</span><span style="color:#F286C4;">func</span><span style="color:#F6F6F4;">(key </span><span style="color:#97E1F1;font-style:italic;">string</span><span style="color:#F6F6F4;">, value </span><span style="color:#97E1F1;font-style:italic;">int</span><span style="color:#F6F6F4;">) </span><span style="color:#97E1F1;font-style:italic;">bool</span><span style="color:#F6F6F4;"> {</span></span>
<span class="line"><span style="color:#F6F6F4;">        fmt.</span><span style="color:#97E1F1;">Println</span><span style="color:#F6F6F4;">(value)</span></span>
<span class="line"><span style="color:#F6F6F4;">        </span><span style="color:#F286C4;">return</span><span style="color:#F6F6F4;"> </span><span style="color:#BF9EEE;">true</span></span>
<span class="line"><span style="color:#F6F6F4;">    })</span></span>
<span class="line"><span style="color:#F6F6F4;">}</span></span></code></pre><pre class="shiki vitesse-light vp-code-light"><code><span class="line"><span style="color:#1E754F;">package</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">import</span><span style="color:#393A34;"> </span><span style="color:#999999;">(</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">fmt</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B5695999;">&quot;</span><span style="color:#59873A;">github.com/duke-git/lancet/v2/maputil</span><span style="color:#B5695999;">&quot;</span></span>
<span class="line"><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#1E754F;">func</span><span style="color:#393A34;"> </span><span style="color:#59873A;">main</span><span style="color:#999999;">()</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#B07D48;">cm</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> maputil</span><span style="color:#999999;">.</span><span style="color:#393A34;">NewConcurrentMap</span><span style="color:#999999;">[</span><span style="color:#AB5959;">string</span><span style="color:#999999;">,</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">int</span><span style="color:#999999;">](</span><span style="color:#2F798A;">100</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">var</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">wg1</span><span style="color:#393A34;"> sync</span><span style="color:#999999;">.</span><span style="color:#393A34;">WaitGroup</span></span>
<span class="line"><span style="color:#393A34;">    wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Add</span><span style="color:#999999;">(</span><span style="color:#2F798A;">5</span><span style="color:#999999;">)</span></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#1E754F;">for</span><span style="color:#393A34;"> </span><span style="color:#B07D48;">i</span><span style="color:#393A34;"> </span><span style="color:#999999;">:=</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">0</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i </span><span style="color:#AB5959;">&lt;</span><span style="color:#393A34;"> </span><span style="color:#2F798A;">5</span><span style="color:#999999;">;</span><span style="color:#393A34;"> i</span><span style="color:#AB5959;">++</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">go</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">n </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">            cm</span><span style="color:#999999;">.</span><span style="color:#998418;">Set</span><span style="color:#999999;">(</span><span style="color:#393A34;">fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Sprintf</span><span style="color:#999999;">(</span><span style="color:#B5695999;">&quot;</span><span style="color:#A65E2B;">%d</span><span style="color:#B5695999;">&quot;</span><span style="color:#999999;">,</span><span style="color:#393A34;"> n</span><span style="color:#999999;">),</span><span style="color:#393A34;"> n</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">            wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Done</span><span style="color:#999999;">()</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#999999;">}(</span><span style="color:#393A34;">i</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">}</span></span>
<span class="line"><span style="color:#393A34;">    wg1</span><span style="color:#999999;">.</span><span style="color:#998418;">Wait</span><span style="color:#999999;">()</span></span>
<span class="line"></span>
<span class="line"></span>
<span class="line"><span style="color:#393A34;">    cm</span><span style="color:#999999;">.</span><span style="color:#998418;">Range</span><span style="color:#999999;">(</span><span style="color:#1E754F;">func</span><span style="color:#999999;">(</span><span style="color:#393A34;">key </span><span style="color:#AB5959;">string</span><span style="color:#999999;">,</span><span style="color:#393A34;"> value </span><span style="color:#AB5959;">int</span><span style="color:#999999;">)</span><span style="color:#393A34;"> </span><span style="color:#AB5959;">bool</span><span style="color:#393A34;"> </span><span style="color:#999999;">{</span></span>
<span class="line"><span style="color:#393A34;">        fmt</span><span style="color:#999999;">.</span><span style="color:#998418;">Println</span><span style="color:#999999;">(</span><span style="color:#393A34;">value</span><span style="color:#999999;">)</span></span>
<span class="line"><span style="color:#393A34;">        </span><span style="color:#1E754F;">return</span><span style="color:#393A34;"> </span><span style="color:#1E754F;">true</span></span>
<span class="line"><span style="color:#393A34;">    </span><span style="color:#999999;">})</span></span>
<span class="line"><span style="color:#999999;">}</span></span></code></pre></div>`,183),d=[t,c,r,y,F,i,E,A,u,B,m];function g(q,f,h,v,D,C){return p(),o("div",null,d)}const K=l(e,[["render",g]]);export{k as __pageData,K as default};
