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
+10.1k Golang : Qt progress dialog example
+3.4k Golang : Convert lines of string into list for delete and insert operation
+8.1k Golang : Fix - does not implement sort.Interface (missing Len method)
+12.9k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+3.9k Golang : If else example and common mistake
+9.1k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+5.8k Android Studio : Rating bar example
+3.7k Golang : Return multiple values from function
+3.6k Python : Create Whois client or function example
+5.4k Golang : Dealing with struct's private part
+10.3k Golang : Convert(cast) uintptr to string example