Complete the code to create a slice with length 3.
s := make([]int, [1]) fmt.Println(len(s))
The make function creates a slice with the specified length. Here, length 3 means the slice has 3 elements.
Complete the code to create a slice with length 3 and capacity 5.
s := make([]int, 3, [1]) fmt.Println(cap(s))
The third argument in make sets the capacity of the slice. Here, capacity 5 means the slice can grow up to 5 elements without reallocating.
Fix the error in the code to print the length of the slice.
s := []int{1, 2, 3}
fmt.Println([1](s))cap instead of len to get length.size or length.The built-in function len returns the length of a slice in Go.
Fill both blanks to create a slice with length 2 and capacity 4, then print length and capacity.
s := make([]int, [1], [2]) fmt.Println(len(s), cap(s))
The first blank is the length (2), and the second blank is the capacity (4). The make function uses these to create the slice.
Fill the blanks to create a slice from an array, then print its length and capacity.
arr := [5]int{1, 2, 3, 4, 5} s := arr[[1]:[2]] fmt.Println(len(s), cap(s))
The slice s starts at index 1 and ends before index 3, so length is 2. Capacity is from start index 1 to end of array index 5, so capacity is 4.