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
+28.9k Golang : Detect (OS) Operating System
+11.8k Golang : How to detect a server/machine network interface capabilities?
+7.6k Golang : Rot13 and Rot5 algorithms example
+22.5k Golang : How to read JPG(JPEG), GIF and PNG files ?
+19.7k Golang : Get current URL example
+20.3k Golang : How to get struct tag and use field name to retrieve data?
+6.1k Golang : Measure execution time for a function
+7.3k Golang : Of hash table and hash map
+11.6k Get form post value in Go
+52.7k Golang : How to get struct field and value by name