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.8k Golang : Normalize unicode strings for comparison purpose
+11.1k Golang : Web routing/multiplex example
+29.1k Golang : Get first few and last few characters from string
+15.2k Golang : Get HTTP protocol version example
+11.2k Google Maps URL parameters configuration
+9.1k Golang : Get curl -I or head data from URL example
+7.5k Golang : Rot13 and Rot5 algorithms example
+18k Golang : Qt image viewer example
+9.8k Golang : Detect number of active displays and the display's resolution
+8.5k Golang : Ackermann function example
+28k Golang : Move file to another directory
+10.6k Golang : ISO8601 Duration Parser example