diff --git a/examples/slices/slices.go b/examples/slices/slices.go index 08916ef..07343d6 100644 --- a/examples/slices/slices.go +++ b/examples/slices/slices.go @@ -9,15 +9,19 @@ 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` + // An uninitialized slice equals to nil and has + // length 0. var s []string - fmt.Println("unassigned:", s, s == nil, len(s) == 0) + fmt.Println("uninit:", 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) + // By default a new slice's capacity is equal to its + // length; if we know the slice is going to grow ahead + // of time, it's possible to pass a capacity explicitly + // as an additional parameter ot `make`. + s = make([]string, 3) fmt.Println("emp:", s, "len:", len(s), "cap:", cap(s)) // We can set and get just like with arrays. diff --git a/examples/slices/slices.hash b/examples/slices/slices.hash index 5844a3d..163b77f 100644 --- a/examples/slices/slices.hash +++ b/examples/slices/slices.hash @@ -1,2 +1,2 @@ -d255a550fe04d6a6d31c7256a1d980e675db0619 -ryiklDqjbId +cbb10033e4e5d2f26e4b57895313a907072a75d9 +bqMChdRGpcl diff --git a/examples/slices/slices.sh b/examples/slices/slices.sh index 925ed82..8f5d1d4 100644 --- a/examples/slices/slices.sh +++ b/examples/slices/slices.sh @@ -1,8 +1,8 @@ # Note that while slices are different types than arrays, # they are rendered similarly by `fmt.Println`. $ go run slices.go -unassigned: [] true true -emp: [ ] len: 3 cap: 5 +uninit: [] true true +emp: [ ] len: 3 cap: 3 set: [a b c] get: c len: 3 diff --git a/public/slices b/public/slices index a6efec5..26a256d 100644 --- a/public/slices +++ b/public/slices @@ -42,7 +42,7 @@ a more powerful interface to sequences than arrays.
package 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) + fmt.Println("uninit:", s, s == nil, len(s) == 0)
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
.
make
.
- s = make([]string, 3, 5) + s = make([]string, 3) fmt.Println("emp:", s, "len:", len(s), "cap:", cap(s))
fmt.Println
.
$ go run slices.go -unassigned: [] true true -emp: [ ] len: 3 cap: 5 +uninit: [] true true +emp: [ ] len: 3 cap: 3 set: [a b c] get: c len: 3 @@ -322,7 +326,7 @@ Go’s other key builtin data structure: maps.