golang

Golang List

A list refers to an ordered data structure with elements are enclosed within a pair of square brackets and separated by a comma. Lists are some of the most useful data structures in modern-day programming. In this Go article, we will explore how it works with Lists.

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:

import "container/list"

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:

typeElementstruct {
    // 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:

package main
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:

&{{0xc00010e480 0xc00010e480 <nil> <nil>} 0}

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:

package main
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:

package main

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:

100

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:

to_remove := my_list.Back() // store reference to the element
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:

&{0xc00010e4b0 0xc00010e480 0xc00010e480 200}

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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list