Menu Close

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

In this tutorial, we are going to learn about Len() function in list package in go golang. Len() function is used to get the total number of elements in list in go golang.

Function proto type:

func (l *List) Len() int 

Len() function:

Len() function returns the number of elements in list.
Complexity of Len() is O(1).

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

Example to use Len() 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)
  l.PushBack(4)

  fmt.Println("Length of list: ", l.Len())
}

Output:

Length of list:  4 

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

https://www.techieindoor.com/go-lang-tutorial/
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. Required fields are marked *