Hello everyone, In this tutorial, we will show you how to work with list and various list examples in Go language. We are going to perform a few scenarios with list object such as
- How to add elements to the list
- How to get size or length of the list
- How to merge the list objects
- How to iterate the list objects
- How to remove all elements from a list
The following examples are well tested and shared in the post.
#1 How to add elements to the list
package main import ( "container/list" ) func main() { // creating a list object listObj1 := list.New() // pushing the elements back to the list listObj1.PushBack("Dinesh") listObj1.PushBack("Krishnan") // pushing the elements front of the list listObj1.PushFront("John") }
#2 How to get size or length of the list
package main import ( "container/list" "fmt" ) func main() { // creating a list object listObj1 := list.New() // pushing the elements back to the list listObj1.PushBack("Dinesh") listObj1.PushBack("Krishnan") // pushing the elements front of the list listObj1.PushFront("John") // getting the size of the list length := listObj1.Len() fmt.Println("The list size is:", length) }
Output
The list size is: 3
#3 How to merge the list objects
package main import ( "container/list" "fmt" ) func main() { // creating a list object listObj1 := list.New() // pushing the elements back to the list listObj1.PushBack("Dinesh") listObj1.PushBack("Krishnan") // pushing the elements front of the list listObj1.PushFront("John") // creating the list object listObj2 := list.New() listObj2.PushFront("Robert") listObj2.PushFront("Smith") // merging two list object to one list listObj1.PushFrontList(listObj2) // getting the size of the list length := listObj1.Len() fmt.Println("The list size is:", length) }
Output
The list size is: 5
#4 How to iterate the list objects
package main import ( "container/list" "fmt" ) func main() { // creating a list object listObj1 := list.New() // pushing the elements back to the list listObj1.PushBack("Dinesh") listObj1.PushBack("Krishnan") // pushing the elements front of the list listObj1.PushFront("John") // creating the list object listObj2 := list.New() listObj2.PushFront("Robert") listObj2.PushFront("Smith") // merging two list object to one list listObj1.PushFrontList(listObj2) // iterating the list for i := listObj1.Front(); i != nil; i = i.Next() { fmt.Println(i.Value) } }
Output
Smith Robert John Dinesh Krishnan
#5 How to remove all elements from a list
package main import ( "container/list" "fmt" ) func main() { // creating a list object listObj1 := list.New() // pushing the elements back to the list listObj1.PushBack("Dinesh") listObj1.PushBack("Krishnan") // pushing the elements front of the list listObj1.PushFront("John") // creating the list object listObj2 := list.New() listObj2.PushFront("Robert") listObj2.PushFront("Smith") // merging two list object to one list listObj1.PushFrontList(listObj2) // removing all elements from list listObj1.Init() if listObj1.Len() == 0 { fmt.Println("List is empty :(") } else { // iterating the list for i := listObj1.Front(); i != nil; i = i.Next() { fmt.Println(i.Value) } } }
Output
List is empty :(
References
- https://golang.org/doc/
- https://golang.org/pkg/
- https://golang.org/pkg/fmt/
- https://golang.org/pkg/fmt/#Println
- https://golang.org/pkg/container/list/
More from my site

Hello, folks, I am a founder of idineshkrishnan.com. I love open source technologies, If you find my tutorials are useful, please consider making donations to these charities.
No responses yet