container/list

Guided tour · Containers · pkg.go.dev →

Doubly-linked list. Useful when you need to insert/remove nodes in O(1) given an element pointer — LRU caches, etc.

Basic operations

Push, iterate, remove

l := list.New()
l.PushBack("a")
l.PushBack("b")
e := l.PushFront("first")
l.InsertAfter("second", e)

for e := l.Front(); e != nil; e = e.Next() {
    fmt.Println(e.Value)
}
l.Remove(e)

LRU cache sketch

A map[K]*list.Element + a list gives you O(1) get/put with O(1) eviction of the tail. This is the canonical use.