In the Go programming language you can create arrays: collections.
You can create slices with the built-in make() function; you can create dynamically-sized arrays this way.
Remember that the usual array has a fixed size, that you would define in one of these two ways:
var a [10]int
var a = []int64{ 1,2,3,4 }
What if you want an array that contains zeroes?
Make() in Go
The make function allocates a zeroed array and returns a slice that refers to that array. The syntax of the make() function is:
your_array := make([]type, length)
So if you'd have a program like this:
package main
import "fmt"
func main() {
a := make([]int, 5)
printSlice("a", a)
}
func printSlice(s string, x []int) {
fmt.Printf("%s len=%d cap=%d %v\n",
s, len(x), cap(x), x)
}
It would output the array (contains a lot of zeros, the make() function does this):
a len=5 cap=5 [0 0 0 0 0]
To change its size, change the second parameter
a := make([]int, 50)
Top comments (0)