create a slice with a length and capacity of type int in go

To create a slice in Go with a specific length and capacity, you can use the make function. The make function takes three arguments: the type of slice, the length, and the capacity.

To create a slice of type int with a length and capacity of 5, you would use the following code:

main.go
mySlice := make([]int, 5, 5)
29 chars
2 lines

The first argument []int specifies the type of the slice, the second argument 5 specifies the length, and the third argument 5 specifies the capacity.

Note that the capacity of the slice determines the amount of space allocated for the slice's underlying array, which can improve performance if you know how much space the slice will need in advance.

gistlibby LogSnag