Complete the code to append the value 5 to the slice.
numbers := []int{1, 2, 3, 4}
numbers = append(numbers, [1])The append function adds the value 5 to the slice numbers.
Complete the code to append all elements from slice b to slice a.
a := []int{1, 2}
b := []int{3, 4}
a = append(a, [1]...)
fmt.Println(a)Using b... appends all elements from slice b to slice a.
Fix the error in the code to append the value 10 to the slice.
var nums []int nums = append([1], 10) fmt.Println(nums)
nil instead of the slice variable as the first argument.You must append to the slice variable nums, not to nil or other values.
Fill both blanks to create a new slice with squares of numbers greater than 3.
nums := []int{1, 2, 3, 4, 5}
squares := []int{}
for _, n := range nums {
if n [1] 3 {
squares = append(squares, n[2]n)
}
}
fmt.Println(squares)The condition n > 3 filters numbers greater than 3, and n * n calculates their squares.
Fill all three blanks to create a map of numbers and their squares for numbers less than 5.
nums := []int{1, 2, 3, 4, 5}
squares := map[int]int{}
for _, n := range nums {
if n [1] 5 {
squares[[2]] = n[3]n
}
}
fmt.Println(squares)The condition n < 5 filters numbers less than 5, n is the key, and n * n is the value in the map.
