Array and Slice behaviors with len() and cap()

Invalid use of make.

./prog.go:8:22: invalid argument: cannot make [4]int; type must be slice, map, or channel

Instead, var arrOne [4]int gives an array of length 4.

These are not arrays, so you shouldn’t name them as such. Go arrays have a size fixed at compile time. Passing an array copies the entire array. Go slices are view of arrays, so thinking of them that way helps to fully understand them. They also happen to be cheap to pass because copying them does not copy the element data. You get a new view of the same backing array.

1 Like