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
+26.9k Golang : Convert file content into array of bytes
+12k Golang : Convert decimal number(integer) to IPv4 address
+6.8k Golang : Check if password length meet the requirement
+5.3k Golang : Calculate half life decay example
+6.4k Unix/Linux : Use netstat to find out IP addresses served by your website server
+38.2k Golang : Read a text file and replace certain words
+6.3k Golang : Extract XML attribute data with attr field tag example
+31k Golang : Interpolating or substituting variables in string examples
+7.8k Golang : Generate human readable password
+9.2k Golang : Handle sub domain with Gin
+8.2k Golang : Variadic function arguments sanity check example
+19.4k Golang : Populate dropdown with html/template example