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 PageSpeed : Clear or flush cache on web server
+4.6k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+14.9k Golang : Get IP addresses of a domain name
+11k Golang : Add ASCII art to command line application launching process
+16.1k Golang : Multi threading or run two processes or more example
+32.3k Golang : All update packages with go get command
+6.7k Golang : Grayscale Image
+10.2k Golang : Post data with url.Values{}
+22.5k Golang : Generate MD5 checksum of a file
+5.6k Elasticsearch : Shutdown a local node
+3.8k Golang : Experimental Jawi programming language
+4.7k Javascript : How to refresh page with JQuery ?