package main import ( "container/list" "fmt" ) func main() { li := list.New() li.PushBack("a") li.PushBack("b") li.PushBack("c") for e := li.Front(); e != nil; e = e.Next() { fmt.Println(e.Value) } }
package main import ( "container/list" "fmt" ) func main() { li := list.New() li.PushBack(1) li.PushBack(2) li.PushBack(3) li.PushBack(4) e := li.PushBack(5) li.MoveBefore(e, li.Front()) for el := li.Front(); el != nil; el = el.Next() { fmt.Print(el.Value, " ") } }In this example, elements 1-4 are added to a linked list as before. The PushBack function is called again to insert a new element 5 to the end of the list. Next, the MoveBefore function is used to move element 5 to the front of the list. Finally, a loop prints the values of each element in the list. Both examples use the container/list package.