In Go, creating slices can be achieved through the following methods:
-
Using the built-in
makefunction:gos := make([]int, 10) // Creates a slice with length and capacity of 10, element type int -
Using literals:
gos := []int{1, 2, 3} // Creates a slice initialized with three elements: 1, 2, 3 -
By using an existing array or slice:
goarr := [5]int{1, 2, 3, 4, 5} s := arr[1:4] // Creates a new slice referencing elements from index 1 to 3 (excluding index 4) in the array
These methods provide flexible ways to create and initialize slices.