Menu Close

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

In this tutorial, we are going to learn about Init() function in list package in go golang. Init() function is used to initialize or clear list in go golang.

Function proto type:

func (l *List) Init() *List 

Init() function:

Init initializes or clears list.

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

Example to use Init() function in list:

package main

import (
  "container/list"
  "fmt"
)

func main() {
  // Create a new list and insert elements in it.
  l := list.New()
        
  l.PushBack(1)
  l.PushBack(2)
  l.PushBack(3)

  // Print length of list
  fmt.Println("Len is", l.Len())

  l.Init()
  
  fmt.Println("Len after applying Init function: ", l.Len())
}

Output:

 Len is 3
Len after applying Init function:  0

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