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.8k Golang : How to validate ISBN?
+4.5k Golang *File points to a file or directory ?
+15.3k Golang : How to extract links from web page ?
+7k Golang : Ways to recover memory during run time.
+9.7k Swift : Convert (cast) String to Integer
+30.1k Golang : Get local IP and MAC address
+34.1k Golang : Strip slashes from string example
+33.5k Golang : Integer is between a range
+41.6k Golang : Get hardware information such as disk, memory and CPU usage
+12.9k Golang : Gin framework accept query string by post request example
+5.6k Unix/Linux : Use netstat to find out IP addresses served by your website server
+13.8k Golang : Missing Bazaar command