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
+15.3k Golang : Count number of digits from given integer value
+9k How to automatically restart your crashed Golang server
+6.3k Golang : Turn string or text file into slice example
+5.3k Golang : Error reading timestamp with GORM or SQL driver
+5.1k Golang : Auto-generate reply email with text/template package
+8.4k Swift : Convert (cast) Float to String
+6.4k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+17.2k Golang : How to run Golang application such as web server in the background or as daemon?
+18.8k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date
+2.8k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+3.9k Golang : Test input string for unicode example