Add example of the new clear() builtin for maps in 1.21
updates #481 Also bump go.mod to 1.21
This commit is contained in:
parent
1ee3369f02
commit
d51586e6bf
@ -39,6 +39,11 @@ func main() {
|
||||
delete(m, "k2")
|
||||
fmt.Println("map:", m)
|
||||
|
||||
// To remove *all* key/value pairs from a map, use
|
||||
// the `clear` builtin.
|
||||
clear(m)
|
||||
fmt.Println("map:", m)
|
||||
|
||||
// The optional second return value when getting a
|
||||
// value from a map indicates if the key was present
|
||||
// in the map. This can be used to disambiguate
|
||||
|
@ -1,2 +1,2 @@
|
||||
0d764945481b4960c573e67f7d303166b750413e
|
||||
xl_teZEVqbo
|
||||
b976324013ef31370d1605d1834c5dd6f0db0ea1
|
||||
6gcPOX9Mm1R
|
||||
|
@ -6,5 +6,6 @@ v1: 7
|
||||
v3: 0
|
||||
len: 2
|
||||
map: map[k1:7]
|
||||
map: map[]
|
||||
prs: false
|
||||
map: map[bar:2 foo:1]
|
||||
|
2
go.mod
2
go.mod
@ -1,6 +1,6 @@
|
||||
module github.com/mmcgrana/gobyexample
|
||||
|
||||
go 1.19
|
||||
go 1.21
|
||||
|
||||
require (
|
||||
github.com/alecthomas/chroma v0.8.2
|
||||
|
20
public/maps
generated
20
public/maps
generated
@ -42,7 +42,7 @@
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="https://go.dev/play/p/xl_teZEVqbo"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="https://go.dev/play/p/6gcPOX9Mm1R"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre>
|
||||
</td>
|
||||
@ -172,6 +172,21 @@ a map.</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>To remove <em>all</em> key/value pairs from a map, use
|
||||
the <code>clear</code> builtin.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<pre class="chroma">
|
||||
<span class="nf">clear</span><span class="p">(</span><span class="nx">m</span><span class="p">)</span>
|
||||
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"map:"</span><span class="p">,</span> <span class="nx">m</span><span class="p">)</span>
|
||||
</pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>The optional second return value when getting a
|
||||
@ -227,6 +242,7 @@ printed with <code>fmt.Println</code>.</p>
|
||||
</span><span class="go">v3: 0
|
||||
</span><span class="go">len: 2
|
||||
</span><span class="go">map: map[k1:7]
|
||||
</span><span class="go">map: map[]
|
||||
</span><span class="go">prs: false
|
||||
</span><span class="go">map: map[bar:2 foo:1]</span></pre>
|
||||
</td>
|
||||
@ -247,7 +263,7 @@ printed with <code>fmt.Println</code>.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' m :\u003D make(map[string]int)\u000A');codeLines.push(' m[\"k1\"] \u003D 7\u000A m[\"k2\"] \u003D 13\u000A');codeLines.push(' fmt.Println(\"map:\", m)\u000A');codeLines.push(' v1 :\u003D m[\"k1\"]\u000A fmt.Println(\"v1:\", v1)\u000A');codeLines.push(' v3 :\u003D m[\"k3\"]\u000A fmt.Println(\"v3:\", v3)\u000A');codeLines.push(' fmt.Println(\"len:\", len(m))\u000A');codeLines.push(' delete(m, \"k2\")\u000A fmt.Println(\"map:\", m)\u000A');codeLines.push(' _, prs :\u003D m[\"k2\"]\u000A fmt.Println(\"prs:\", prs)\u000A');codeLines.push(' n :\u003D map[string]int{\"foo\": 1, \"bar\": 2}\u000A fmt.Println(\"map:\", n)\u000A}\u000A');codeLines.push('');
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' m :\u003D make(map[string]int)\u000A');codeLines.push(' m[\"k1\"] \u003D 7\u000A m[\"k2\"] \u003D 13\u000A');codeLines.push(' fmt.Println(\"map:\", m)\u000A');codeLines.push(' v1 :\u003D m[\"k1\"]\u000A fmt.Println(\"v1:\", v1)\u000A');codeLines.push(' v3 :\u003D m[\"k3\"]\u000A fmt.Println(\"v3:\", v3)\u000A');codeLines.push(' fmt.Println(\"len:\", len(m))\u000A');codeLines.push(' delete(m, \"k2\")\u000A fmt.Println(\"map:\", m)\u000A');codeLines.push(' clear(m)\u000A fmt.Println(\"map:\", m)\u000A');codeLines.push(' _, prs :\u003D m[\"k2\"]\u000A fmt.Println(\"prs:\", prs)\u000A');codeLines.push(' n :\u003D map[string]int{\"foo\": 1, \"bar\": 2}\u000A fmt.Println(\"map:\", n)\u000A}\u000A');codeLines.push('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
Loading…
x
Reference in New Issue
Block a user