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
+14.9k Golang : package is not in GOROOT during compilation
+12.5k Android Studio : Highlight ImageButton when pressed on example
+14.4k Golang : Reset buffer example
+19k Golang : Calculate entire request body length during run time
+11.9k Golang : Save webcamera frames to video file
+29.2k Golang : JQuery AJAX post data to server and send data back to client example
+8.9k Golang : Capture text return from exec function example
+18.1k Golang : How to remove certain lines from a file
+10.5k Golang : Flip coin example
+10.2k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+16k Golang : How to check if input from os.Args is integer?
+12.3k Elastic Search : Return all records (higher than default 10)