In Go, you can initialize a slice with values by using the make()
function or using a slice literal.
make()
function:slice := make([]int, 3) // creates a slice of length 3 with 0 as the default value
You can then assign values to individual elements of the slice using indexing.
slice[0] = 1
slice[1] = 2
slice[2] = 3
slice := []int{1, 2, 3} // creates a slice with initial values 1, 2, and 3
Both methods will result in a slice with values [1, 2, 3]
.