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
+9.1k Golang : Timeout example
+5.7k Golang : Use NLP to get sentences for each paragraph example
+7k Golang : Check if one string(rune) is permutation of another string(rune)
+5.1k Unix/Linux/MacOSx : How to remove an environment variable ?
+17.3k Golang : Defer function inside init()
+9.4k Golang : Format strings to SEO friendly URL example
+35k Golang : Strip slashes from string example
+14.2k Golang : How to check if your program is running in a terminal
+16.8k Golang : How to save log messages to file?
+7.2k Golang : Create zip/ePub file without compression(use Store algorithm)
+12.3k Golang : Exit, terminating or aborting a program