Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang

What is the len function in Golang?

Talha Ashar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The len function in Golang is a built-in function that returns the length of a provided parameter, depending on the parameter’s type.

The prototype of the len function is shown below:

func len(v Type) int

Parameters

The len function takes a single mandatory parameter, v, that can be a string, array, slice, map, or channel.

Return value

The len function returns one of the following, depending on the type of v:

  • Array: If v is an array, then the len function returns the number of elements in v.
  • Pointer to an array: If v is a pointer to an array, then the len function returns the number of elements at the location pointed to by v, even if v is nil.
  • Slice, or map: If v is a slice or map, then the len function returns the number of elements in v. If v is nil, 00 is returned.
  • String: The len function returns the number of bytes in v.
  • Channel: The len function returns the number of queued elements in the buffer. If v is nil, 00 is returned.

Code

The code below shows how the len function works in Golang:

package main
import (
"fmt"
)
func main() {
//initializing variables
a := "Hello World"
b := [6]int{2, 3, 5, 7, 11, 13}
var c []int
//computing lengths
a_length := len(a)
b_length := len(b)
//printing results
fmt.Println("The length of a is: ", a_length)
fmt.Println("The length of b is: ", b_length)
fmt.Println("The length of c is: ", len(c))
}

Explanation

First, the code initializes a string (a), an array (b) that contains 66 elements, and an empty slice (c).

The len function proceeds to compute the length of each of these variables and outputs the results accordingly.

Since a is a string, the len function returns the number of characters it contains, i.e., 1111. Similarly, as b is an array, the len function returns the number of elements it contains, i.e., 66. Finally, since c is a nil slice, the len function returns 00.

RELATED TAGS

golang

CONTRIBUTOR

Talha Ashar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring