Junior — Middle
How can two arrays or lists be combined into one?
sobes.tech AI
Answer from AI
In Go, to combine two slices, you can use the built-in append function. It allows you to add all elements of one slice to another.
Example of combining two slices:
package main
import "fmt"
func main() {
a := []int{1, 2, 3}
b := []int{4, 5, 6}
c := append(a, b...)
fmt.Println(c) // Output: [1 2 3 4 5 6]
}
Here, b... is the syntax for unpacking the slice b into individual elements for the append function.
For arrays, you first need to convert them into slices, then combine using append.