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
+11.6k Golang : Delay or limit HTTP requests example
+41.2k Golang : How to check if a string contains another sub-string?
+16.1k Golang : Get digits from integer before and after given position example
+31k Golang : Download file example
+13k Swift : Convert (cast) Int or int32 value to CGFloat
+7.3k Golang : Array mapping with Interface
+7.7k Golang : How to stop user from directly running an executable file?
+5.8k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+18.3k Golang : Check if a directory exist or not
+7.6k Golang : Gorrila set route name and get the current route name
+8.4k Golang : Add build version and other information in executables