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
+5.1k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+2.3k PHP : Hide PHP version information from curl
+2.9k Get Facebook friends working in same company
+4.6k How to tell if a binary(executable) file or web application is built with Golang?
+1.8k Golang : Find the shortest line of text example
+5k Android Studio : Password input and reveal password example
+2k Javascript : Put image into Chrome browser's console
+2.7k Golang : Muxing with Martini example
+2k Golang : Gonum standard normal random numbers example
+4.8k Use systeminfo to find out installed Windows Hotfix(s) or updates
+10.2k Golang : convert(cast) string to float value
+4.6k Golang : How to determine a prime number?