OneBite.Dev - Coding blog in a bite size

merge two arrays together in Go

Code snippet on how to merge two arrays together in Go

arr1 := []string{"a", "b", "c"}
arr2 := []string{"1", "2"}

arr3 := make([]string, 0)
arr3 = append(arr3, arr1...)
arr3 = append(arr3, arr2...)

This code snippet merges two string arrays arr1 and arr2 into a third array arr3. First, it initializes an empty array called arr3. Then it appends the elements from arr1 by using the ellipsis operator. The ellipsis operator lets you expand a slice into individual elements. After that, the elements from the second array, arr2, are added to the end of arr3. Finally, the result is a combination of both the inputs, arr1 and arr2, in arr3.

go