golang make () of the third parameter

Learning for some time golang, probably can do some small things with golang, then look back at some of the basic things. golang allocate memory have a make function, the first parameter of the function is type, and the second parameter is the space allocated, the third parameter is reserved for allocation of space, the first two parameters are well understood, but my third ignorant force parameter has a look, for example a: = make ([] int, 5, 10), len (a) output is 5, cap (a) output is 10, then the assignment of I a [4] can be found too, but for a [5] found that the assignment error, so depressed this space reserved for allocation to how to use it, then google a bit and found that the need to re-slice the reserved space before you can use, so do some recorded code is as follows.

package main

import "fmt"

func main(){ a := make([]int, 10, 20) fmt.Printf("%d, %d\n", len(a), cap(a)) fmt.Println(a) b := a[:cap(a)] fmt.Println(b) }

Output

10, 20
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

Guess you like

Origin www.cnblogs.com/ExMan/p/11445754.html