Golang : Iterating Elements Over A List
In this short tutorial, we will learn how to iterate the elements over a list. The code below will populate the list first and then perform a "next" scan and then a "prev" scan to list out the elements inside the list.
package main
import (
"container/list"
"fmt"
)
func main() {
alist := list.New()
alist.PushBack("a")
alist.PushBack("b")
alist.PushBack("c")
fmt.Println("Next")
for e := alist.Front(); e != nil; e = e.Next() {
fmt.Println(e.Value) // print out the elements
}
fmt.Println("---------")
fmt.Println("Prev")
for e := alist.Back(); e != nil; e = e.Prev() {
fmt.Println(e.Value) // print out the elements
}
}
Output :
Next
a
b
c
---------
Prev
c
b
a
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
+4.6k Mac OSX : Get disk partitions' size, type and name
+4.8k Python : Find out the variable type and determine the type with simple test
+32.9k Golang : How to check if a date is within certain range?
+7.4k Golang : Dealing with struct's private part
+18.2k Golang : How to remove certain lines from a file
+7.1k Golang : Get environment variable
+24.4k Golang : Time slice or date sort and reverse sort example
+6.7k Golang : Output or print out JSON stream/encoded data
+5.9k AWS S3 : Prevent Hotlinking policy
+9.4k Golang : Convert(cast) string to int64
+9.8k Golang : Get current, epoch time and display by year, month and day
+15.5k Golang : ROT47 (Caesar cipher by 47 characters) example