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.9k Golang : How to pass data between controllers with JSON Web Token
+8.2k Golang : Multiplexer with net/http and map
+14.1k Golang : convert rune to unicode hexadecimal value and back to rune character
+9k Golang : Gaussian blur on image and camera video feed examples
+21.4k Golang : Clean up null characters from input data
+8.2k Golang : Get all countries phone codes
+10.6k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+12.5k Golang : Validate email address
+46.6k Golang : Encode image to base64 example
+23.3k Golang : Randomly pick an item from a slice/array example
+15.7k Golang : rune literal not terminated error