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 Unix/Linux/MacOSx : How to remove an environment variable ?
+6.5k Mac/Linux/Windows : Get CPU information from command line
+4.9k Golang : Get FX sentiment from website example
+8.6k Golang : Get SPF and DMARC from email headers to fight spam
+20.6k Golang : For loop continue,break and range
+85k Golang : How to convert character to ASCII and back
+6.8k Golang : Gorrila mux.Vars() function example
+13.7k Golang : syscall.Socket example
+4.5k Fix Google Analytics Redundant Hostnames problem
+37.4k Golang : Read a text file and replace certain words
+23.4k Golang : Upload to S3 with official aws-sdk-go package
+4.6k Google : Block or disable caching of your website content