Go – How to Work with List in Go Language

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

  1. How to add elements to the list
  2. How to get size or length of the list
  3. How to merge the list objects
  4. How to iterate the list objects
  5. 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

  1. https://golang.org/doc/
  2. https://golang.org/pkg/
  3. https://golang.org/pkg/fmt/
  4. https://golang.org/pkg/fmt/#Println
  5. https://golang.org/pkg/container/list/