Golang container/list.List.InsertBefore() function example

package container/list

InsertBefore inserts a new element with given value immediately before mark and returns the new element. If mark is not an element of the list, the list is not modified.

Golang container/list.List.InsertBefore() function usage example

 package main

  import (
 "container/list"
 "fmt"
  )

  func main() {
 alist := list.New()
 insertelem := alist.PushFront("a")
 alist.PushFront("c")

 alist.InsertBefore("b",insertelem)

 for e := alist.Front(); e != nil; e = e.Next() {
 fmt.Println(e.Value) // print out the elements
 }
 }

Output :

c

b

a

Reference :

http://golang.org/pkg/container/list/#List.InsertBefore

Advertisement