add unassigned slice example (#475)

This commit is contained in:
jiangbo 2023-05-17 02:04:20 +08:00 committed by GitHub
parent fcc06e2fe1
commit fd4d72bccf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 13 deletions

View File

@ -9,11 +9,16 @@ func main() {
// Unlike arrays, slices are typed only by the // Unlike arrays, slices are typed only by the
// elements they contain (not the number of elements). // elements they contain (not the number of elements).
// unassigned slice equal to nil and has a length of `0`
var s []string
fmt.Println("unassigned:", s, s == nil, len(s) == 0)
// To create an empty slice with non-zero length, use // To create an empty slice with non-zero length, use
// the builtin `make`. Here we make a slice of // the builtin `make`. Here we make a slice of
// `string`s of length `3` (initially zero-valued). // `string`s of length `3` (initially zero-valued).
s := make([]string, 3) // `string`s of capacity `5`.
fmt.Println("emp:", s) s = make([]string, 3, 5)
fmt.Println("emp:", s, "len:", len(s), "cap:", cap(s))
// We can set and get just like with arrays. // We can set and get just like with arrays.
s[0] = "a" s[0] = "a"

View File

@ -1,2 +1,2 @@
13835b88336e031808f2f3887cd43d0b9d85cad0 d255a550fe04d6a6d31c7256a1d980e675db0619
76f4Jif5Z8o ryiklDqjbId

View File

@ -1,7 +1,8 @@
# Note that while slices are different types than arrays, # Note that while slices are different types than arrays,
# they are rendered similarly by `fmt.Println`. # they are rendered similarly by `fmt.Println`.
$ go run slices.go $ go run slices.go
emp: [ ] unassigned: [] true true
emp: [ ] len: 3 cap: 5
set: [a b c] set: [a b c]
get: c get: c
len: 3 len: 3

32
public/slices generated
View File

@ -42,7 +42,7 @@ a more powerful interface to sequences than arrays.</p>
</td> </td>
<td class="code leading"> <td class="code leading">
<a href="https://go.dev/play/p/76f4Jif5Z8o"><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/ryiklDqjbId"><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 class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre> </pre>
</td> </td>
@ -74,16 +74,31 @@ a more powerful interface to sequences than arrays.</p>
<td class="docs"> <td class="docs">
<p>Unlike arrays, slices are typed only by the <p>Unlike arrays, slices are typed only by the
elements they contain (not the number of elements). elements they contain (not the number of elements).
To create an empty slice with non-zero length, use unassigned slice equal to nil and has a length of <code>0</code></p>
the builtin <code>make</code>. Here we make a slice of
<code>string</code>s of length <code>3</code> (initially zero-valued).</p>
</td> </td>
<td class="code leading"> <td class="code leading">
<pre class="chroma"> <pre class="chroma">
<span class="nx">s</span> <span class="o">:=</span> <span class="nb">make</span><span class="p">([]</span><span class="kt">string</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="kd">var</span> <span class="nx">s</span> <span class="p">[]</span><span class="kt">string</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">&#34;emp:&#34;</span><span class="p">,</span> <span class="nx">s</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">&#34;unassigned:&#34;</span><span class="p">,</span> <span class="nx">s</span><span class="p">,</span> <span class="nx">s</span> <span class="o">==</span> <span class="kc">nil</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="nx">s</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>To create an empty slice with non-zero length, use
the builtin <code>make</code>. Here we make a slice of
<code>string</code>s of length <code>3</code> (initially zero-valued).
<code>string</code>s of capacity <code>5</code>.</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">s</span> <span class="p">=</span> <span class="nb">make</span><span class="p">([]</span><span class="kt">string</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</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">&#34;emp:&#34;</span><span class="p">,</span> <span class="nx">s</span><span class="p">,</span> <span class="s">&#34;len:&#34;</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="nx">s</span><span class="p">),</span> <span class="s">&#34;cap:&#34;</span><span class="p">,</span> <span class="nb">cap</span><span class="p">(</span><span class="nx">s</span><span class="p">))</span>
</pre> </pre>
</td> </td>
</tr> </tr>
@ -252,7 +267,8 @@ they are rendered similarly by <code>fmt.Println</code>.</p>
<pre class="chroma"> <pre class="chroma">
<span class="gp">$</span> go run slices.go <span class="gp">$</span> go run slices.go
<span class="go">emp: [ ] <span class="go">unassigned: [] true true
</span><span class="go">emp: [ ] len: 3 cap: 5
</span><span class="go">set: [a b c] </span><span class="go">set: [a b c]
</span><span class="go">get: c </span><span class="go">get: c
</span><span class="go">len: 3 </span><span class="go">len: 3
@ -306,7 +322,7 @@ Go&rsquo;s other key builtin data structure: maps.</p>
</div> </div>
<script> <script>
var codeLines = []; var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' s :\u003D make([]string, 3)\u000A fmt.Println(\"emp:\", s)\u000A');codeLines.push(' s[0] \u003D \"a\"\u000A s[1] \u003D \"b\"\u000A s[2] \u003D \"c\"\u000A fmt.Println(\"set:\", s)\u000A fmt.Println(\"get:\", s[2])\u000A');codeLines.push(' fmt.Println(\"len:\", len(s))\u000A');codeLines.push(' s \u003D append(s, \"d\")\u000A s \u003D append(s, \"e\", \"f\")\u000A fmt.Println(\"apd:\", s)\u000A');codeLines.push(' c :\u003D make([]string, len(s))\u000A copy(c, s)\u000A fmt.Println(\"cpy:\", c)\u000A');codeLines.push(' l :\u003D s[2:5]\u000A fmt.Println(\"sl1:\", l)\u000A');codeLines.push(' l \u003D s[:5]\u000A fmt.Println(\"sl2:\", l)\u000A');codeLines.push(' l \u003D s[2:]\u000A fmt.Println(\"sl3:\", l)\u000A');codeLines.push(' t :\u003D []string{\"g\", \"h\", \"i\"}\u000A fmt.Println(\"dcl:\", t)\u000A');codeLines.push(' twoD :\u003D make([][]int, 3)\u000A for i :\u003D 0; i \u003C 3; i++ {\u000A innerLen :\u003D i + 1\u000A twoD[i] \u003D make([]int, innerLen)\u000A for j :\u003D 0; j \u003C innerLen; j++ {\u000A twoD[i][j] \u003D i + j\u000A }\u000A }\u000A fmt.Println(\"2d: \", twoD)\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push(''); codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import \"fmt\"\u000A');codeLines.push('func main() {\u000A');codeLines.push(' var s []string\u000A fmt.Println(\"unassigned:\", s, s \u003D\u003D nil, len(s) \u003D\u003D 0)\u000A');codeLines.push(' s \u003D make([]string, 3, 5)\u000A fmt.Println(\"emp:\", s, \"len:\", len(s), \"cap:\", cap(s))\u000A');codeLines.push(' s[0] \u003D \"a\"\u000A s[1] \u003D \"b\"\u000A s[2] \u003D \"c\"\u000A fmt.Println(\"set:\", s)\u000A fmt.Println(\"get:\", s[2])\u000A');codeLines.push(' fmt.Println(\"len:\", len(s))\u000A');codeLines.push(' s \u003D append(s, \"d\")\u000A s \u003D append(s, \"e\", \"f\")\u000A fmt.Println(\"apd:\", s)\u000A');codeLines.push(' c :\u003D make([]string, len(s))\u000A copy(c, s)\u000A fmt.Println(\"cpy:\", c)\u000A');codeLines.push(' l :\u003D s[2:5]\u000A fmt.Println(\"sl1:\", l)\u000A');codeLines.push(' l \u003D s[:5]\u000A fmt.Println(\"sl2:\", l)\u000A');codeLines.push(' l \u003D s[2:]\u000A fmt.Println(\"sl3:\", l)\u000A');codeLines.push(' t :\u003D []string{\"g\", \"h\", \"i\"}\u000A fmt.Println(\"dcl:\", t)\u000A');codeLines.push(' twoD :\u003D make([][]int, 3)\u000A for i :\u003D 0; i \u003C 3; i++ {\u000A innerLen :\u003D i + 1\u000A twoD[i] \u003D make([]int, innerLen)\u000A for j :\u003D 0; j \u003C innerLen; j++ {\u000A twoD[i][j] \u003D i + j\u000A }\u000A }\u000A fmt.Println(\"2d: \", twoD)\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
</script> </script>
<script src="site.js" async></script> <script src="site.js" async></script>
</body> </body>