DEV Community

Cover image for Slice of cake
Jorge Araya
Jorge Araya

Posted on

Slice of cake

Is there a difference between slices and arrays? Well, if there is any, it is a difference of relations. slice depends on array, when you declare a slice in Go you get an underlying array associated with your slice, if you have several slices of the same type they may even share storage with the underlying array.

How do you create a slice?

You can use the same facilities for declaring arrays in Go, with the optional argument for "capacity" provided:

make([]int, 50, 100)
Enter fullscreen mode Exit fullscreen mode

This is the same as creating an array and slicing it:

new([100]int)[0:50]
Enter fullscreen mode Exit fullscreen mode

read more

Top comments (0)