Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Go

Idiom #166 Concatenate two lists

Create the list ab containing all the elements of the list a, followed by all the elements of the list b.

ab := append(a, b...)

Warning! a and ab may or may not share the same underlying memory.
var ab []T
ab = append(append(ab, a...), b...)

This ensures that ab is a new list (not sharing any memory with a and b).
T is the type of the elements.
ab := make([]T, len(a)+len(b))
copy(ab, a)
copy(ab[len(a):], b)
(def ab (concat a b))

New implementation...