Golang : Linked list example
In Golang, I would say that slices superseded list.... as slices would allow you to dynamically resize, pop, push, cut, delete and copy. However, if you still want to use list for LIFO-FIFO stuff... you can use the container/list
package.
Here is a linked list example in Golang.
package main
import (
"container/list"
"fmt"
)
func main() {
// create a new link list
alist := list.New()
fmt.Println("Size before : ", alist.Len()) // list size before
// push element into list
alist.PushBack("a")
alist.PushBack("b")
alist.PushBack("c")
fmt.Println("Size after insert(push): ", alist.Len()) // list size after
// list elements
for e := alist.Front(); e != nil; e = e.Next() {
fmt.Println(e.Value.(string))
}
// pop 3 elements
alist.Remove(alist.Front())
alist.Remove(alist.Front())
alist.Remove(alist.Front())
fmt.Println("Size after remove(pop) : ", alist.Len()) // list size after
}
Output :
Size before : 0
Size after insert(push): 3
a
b
c
Size after remove(pop) : 0
References :
https://www.socketloop.com/references/golang-container-list-new-function-example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.7k Golang : Scan files for certain pattern and rename part of the files
+4.1k Golang : Calculate half life decay example
+35.2k Golang : Convert(cast) int64 to string
+6.8k Golang : Get all countries phone codes
+7.5k Golang : Gaussian blur on image and camera video feed examples
+4.6k Golang : Markov chains to predict probability of next state example
+7.8k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+5.3k Grep : How to grep for strings inside binary data
+11.6k Android Studio : Password input and reveal password example
+15.3k CodeIgniter/PHP : Create directory if does not exist example
+27.7k Golang : How to create new XML file ?
+7.4k Golang : Count leading or ending zeros(any item of interest) example