Menu Close

Go – PushBack() function in list package in go golang

In this tutorial, we are going to learn about PushBack() function in list package in go golang. PushBack() function is used to insert list node at the end of the list in go golang.

Function proto type:

func (l *List) PushBack(v interface{}) *Element

PushBack() function:

PushBack() function insert element at the end of the list.
PushBack() function returns the pointer of newly inserted element. 

Example:

list = 1 -> 2

After apply PushBack() in list with element 3

list.PushBack(3)

list = 1 -> 2 -> 3

To learn more about list package in go, Please follow this link

Example to use PushBack() function in list:

package main

import (
  "container/list"
  "fmt"
)

func main() {
    
  var ele *list.Element

  // Create a new list and insert elements in it.
  l := list.New()

  l.PushBack(1) // 1
  l.PushBack(2) // 1 -> 2

  fmt.Println("Print list")
  for ele = l.Front(); ele != nil; ele = ele.Next() {

    fmt.Println(ele.Value)
    
  }
}

Output:

 Print list
1
2

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

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/
Posted in golang, list package

Leave a Reply

Your email address will not be published.

Contact Us