Magpie dev
articles
Appending and prepending golang slices
Sep 10th, 2019

Quick tip

Take a simple slice of structs:

type Post struct {
  Name string
  ID   int
}

var list []Post

Appending uses the builtin keyword append

var list []Post
item1 := Post{Name: "first", ID: 0}
item2 := Post{Name: "second", ID: 1}

list = append(list, item1)
len(list)
// > 1

list = append(list, item2)

len(list)
// > 2

Prepending

This is where things get interesting.

The append function "merges" or conacts an item to a slice.

Type signature:

func append(slice []Type, elems ...Type) []Type

By inverting the order and creating a slice with a single element we can use a variadic interface to destructure the original slice.


item1 := Post{Name: "first", ID: 0}
item2 := Post{Name: "second", ID: 1}

list = append(list, item1)

fmt.Println(list[0])
// {0, first}


list = append([]Post{item2}, list...)
fmt.Println(list[0])
// {1, second}

Happy affixing

Appending/Prepending Map slices in go

MAGPIE DEV articles by:

Stephen Lacy

logo magpie icon by
Philipp Lehmann

© 2017 magpiedev