From 9ac4975fc00608939343c8eae67774831eb99512 Mon Sep 17 00:00:00 2001
From: jiangbo
Date: Tue, 16 May 2023 23:10:40 +0800
Subject: [PATCH] add unassigned slice example
---
examples/slices/slices.go | 9 +++++++--
examples/slices/slices.hash | 4 ++--
examples/slices/slices.sh | 3 ++-
public/slices | 32 ++++++++++++++++++++++++--------
4 files changed, 35 insertions(+), 13 deletions(-)
diff --git a/examples/slices/slices.go b/examples/slices/slices.go
index c9335e1..08916ef 100644
--- a/examples/slices/slices.go
+++ b/examples/slices/slices.go
@@ -9,11 +9,16 @@ func main() {
// Unlike arrays, slices are typed only by the
// 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
// the builtin `make`. Here we make a slice of
// `string`s of length `3` (initially zero-valued).
- s := make([]string, 3)
- fmt.Println("emp:", s)
+ // `string`s of capacity `5`.
+ s = make([]string, 3, 5)
+ fmt.Println("emp:", s, "len:", len(s), "cap:", cap(s))
// We can set and get just like with arrays.
s[0] = "a"
diff --git a/examples/slices/slices.hash b/examples/slices/slices.hash
index e23e052..5844a3d 100644
--- a/examples/slices/slices.hash
+++ b/examples/slices/slices.hash
@@ -1,2 +1,2 @@
-13835b88336e031808f2f3887cd43d0b9d85cad0
-76f4Jif5Z8o
+d255a550fe04d6a6d31c7256a1d980e675db0619
+ryiklDqjbId
diff --git a/examples/slices/slices.sh b/examples/slices/slices.sh
index f29dd36..925ed82 100644
--- a/examples/slices/slices.sh
+++ b/examples/slices/slices.sh
@@ -1,7 +1,8 @@
# Note that while slices are different types than arrays,
# they are rendered similarly by `fmt.Println`.
$ go run slices.go
-emp: [ ]
+unassigned: [] true true
+emp: [ ] len: 3 cap: 5
set: [a b c]
get: c
len: 3
diff --git a/public/slices b/public/slices
index 78c327a..a6efec5 100644
--- a/public/slices
+++ b/public/slices
@@ -42,7 +42,7 @@ a more powerful interface to sequences than arrays.
- 
+ 
package main
|
@@ -74,16 +74,31 @@ a more powerful interface to sequences than arrays.
Unlike arrays, slices are typed only by the
elements they contain (not the number of elements).
-To create an empty slice with non-zero length, use
-the builtin make . Here we make a slice of
-string s of length 3 (initially zero-valued).
+unassigned slice equal to nil and has a length of 0
|
- s := make([]string, 3)
- fmt.Println("emp:", s)
+ var s []string
+ fmt.Println("unassigned:", s, s == nil, len(s) == 0)
+
+ |
+
+
+
+
+ To create an empty slice with non-zero length, use
+the builtin make . Here we make a slice of
+string s of length 3 (initially zero-valued).
+string s of capacity 5 .
+
+ |
+
+
+
+ s = make([]string, 3, 5)
+ fmt.Println("emp:", s, "len:", len(s), "cap:", cap(s))
|
@@ -252,7 +267,8 @@ they are rendered similarly by fmt.Println
.
$ go run slices.go
-emp: [ ]
+unassigned: [] true true
+emp: [ ] len: 3 cap: 5
set: [a b c]
get: c
len: 3
@@ -306,7 +322,7 @@ Go’s other key builtin data structure: maps.