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
+18.5k Golang : How to count the number of repeated characters in a string?
+4.4k PHP : Fix Call to undefined function curl_init() error
+11k Golang : How to parse plain email text and process email header?
+6.7k Golang : How to execute code at certain day, hour and minute?
+6.2k Golang : Of hash table and hash map
+8.9k Golang : Convert octal value to string to deal with leading zero problem
+4.7k Golang : Markov chains to predict probability of next state example
+5.1k AWS S3 : Prevent Hotlinking policy
+23.4k Golang : Change file read or write permission example
+11.4k Swift : Convert (cast) Int or int32 value to CGFloat
+6.3k Golang : Calculate how many weeks left to go in a given year
+4.7k Swift : Get substring with rangeOfString() function example