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
+4.5k PHP : Hide PHP version information from curl
+8.7k Golang : Validate IPv6 example
+16.5k Golang : Read data from config file and assign to variables
+14.2k Golang : Validate hostname
+7k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+24.7k Mac/Linux and Golang : Fix bind: address already in use error
+17.5k Golang : Set, Get and List environment variables
+6k Golang : Levenshtein distance example
+4.4k Golang : How to deal with configuration data?
+5.4k PHP : Proper way to get UTF-8 character or string length
+8.6k Golang : Copy map(hash table) example
+12.2k Golang : Generate Code128 barcode