concept slice literal in category go

appears as: slice literals, slice literal
Go in Action

This is an excerpt from Manning's book Go in Action.

An idiomatic way of creating a slice is to use a slice literal. It’s similar to creating an array, except you don’t specify a value inside of the [ ] operator. The initial length and capacity will be based on the number of elements you initialize.

Listing 4.19. Declaring a slice with a slice literal
// Create a slice of strings. // Contains a length and capacity of 5 elements. slice := []string{"Red", "Blue", "Green", "Yellow", "Pink"} // Create a slice of integers. // Contains a length and capacity of 3 elements. slice := []int{10, 20, 30}

When using a slice literal, you can set the initial length and capacity. All you need to do is initialize the index that represents the length and capacity you need. The following syntax will create a slice with a length and capacity of 100 elements.