Golang Container/List
In Go programming, lists are provided using the container/list package. It is a very handy package that comes with multiple tools and functions to work with a list data structure.
Before using this package, we need to import it:
The package provides us with two structs to implement a list item and the actual list. These methods are Element and List, as shown in the following syntax:
// The value stored with this element.
Value interface{}
// contains filtered or unexported fields
}
typeListstruct {
// contains filtered or unexported fields
}
Golang Declare List
We can initialize an empty array using the new method from the container/list package. An example is as shown:
import (
"container/list"
"fmt"
)
funcmain() {
my_list := list.New()
fmt.Println(my_list)
}
The previous code starts by importing the required packages. In this example, we import the container/list and fmt to work with lists and I/O, respectively.
If we run the previous code, it should return an output as shown below:
Golang Add List Items
Once we have an empty list declared, we can add items to the list using the PushBack and PushFront methods. The PushFront() method adds a new element to the beginning of a list, while the PushBack() method adds an item at the end of the list.
An example is as shown below:
import (
"container/list"
"fmt"
)
funcmain() {
my_list := list.New()
my_list.PushBack(200)
my_list.PushFront(100)
}
The previous code uses the PushBack and PushFront methods to add elements to the front and end of a list.
Golang Get List
To view the items in a list, we can use the Front() and Back() methods to get the first and last element, respectively.
If the list is empty, the functions return a nil value:
import (
"container/list"
"fmt"
)
funcmain() {
my_list := list.New()
my_list.PushBack(200)
my_list.PushFront(100)
fori := my_list.Front(); i != nil; i = i.Next() {
fmt.Println(i.Value)
}
}
In the previous code, we used a for loop to get the items of a list. We start by processing the first node of the list (list.Front()) and iterate over the following items in the list. Once the loop encounters the end of the list v(nil), it returns the values of i.
The previous code returns an output is as shown:
200
Golang Remove List Items
To remove an item from a list, we can pass the element’s pointers within the list to the Remove() method. It takes care of the deletion process for us.
An example is as follows:
my_list.Remove(to_remove)
fmt.Println(my_list.Front().Value)
In the snippet above, we start by storing the reference to the pointer using the Back() method. Then, we pass the reference pointer to the Remove method.
If you check the value of the “to_remove” variable, you will notice that it’s a pointer as shown:
Conclusion
This denotes the end of our tutorial. Throughout this guide, you learned the fundamentals of working with lists in Go programming and its different effective methods. We hope you found this article helpful. You can check other Linux Hint articles and the package docs to learn more tips.