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
+32.6k Golang : convert(cast) bytes to string
+4.9k Java : Human readable password generator
+13.5k Golang : Missing Bazaar command
+17.2k Golang : Write file with io.WriteString
+7.7k Golang : Find duplicate files with filepath.Walk
+9.6k Golang : Command line file upload program to server example
+4.5k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+7k Golang : Auto-generate reply email with text/template package
+19.7k Golang : Get password from console input without echo or masked
+14.1k Golang : How to add color to string?
+4.7k PHP : Get client IP address