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

package container/list

Remove removes element (e) from list if (e) is an element of the list. It returns the element value e.Value.

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

 package main

 import (
 "container/list"
 "fmt"
 )

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

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


  elementFromAList := alist.Front()

  removed := alist.Remove(elementFromAList)
  fmt.Printf("Removed element [ %s ] from list\n", removed)

  fmt.Println("List After Removal : ")

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

 }

Output :

Elements in List :

d

c

b

a

Removed element [ d ] from list

List After Removal :

c

b

a

Reference :

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

Advertisement