add unassigned slice example (#475)
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
13835b88336e031808f2f3887cd43d0b9d85cad0
|
||||
76f4Jif5Z8o
|
||||
d255a550fe04d6a6d31c7256a1d980e675db0619
|
||||
ryiklDqjbId
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user