Golang len function
last modified May 8, 2025
This tutorial explains how to use the len built-in function in Go.
We'll cover basic usage with practical examples for different data types.
The len function is used to get the length of various types in Go. It works with strings, arrays, slices, maps, and channels. The return value is always an int representing the number of elements.
In Go, len is a built-in function that provides a consistent way
to measure size across different data structures. It's evaluated at compile
time for arrays and at runtime for dynamic types like slices.
Basic string length example
The simplest use of len measures the length of a string in bytes.
This example demonstrates basic string length calculation.
Note: For Unicode strings, len counts bytes, not characters.
package main
import "fmt"
func main() {
str := "Hello, 世界"
fmt.Println("String:", str)
fmt.Println("Length in bytes:", len(str))
// To count runes (Unicode code points) instead:
fmt.Println("Length in runes:", len([]rune(str)))
}
The string contains ASCII and Unicode characters. The byte length differs from the character count due to UTF-8 encoding of non-ASCII characters.
Array and slice length
We can use len to get the length of arrays and slices. This example
shows length calculation for both static and dynamic collections.
package main
import "fmt"
func main() {
// Array (fixed size)
arr := [5]int{1, 2, 3, 4, 5}
fmt.Println("Array length:", len(arr))
// Slice (dynamic size)
slice := []int{1, 2, 3}
fmt.Println("Initial slice length:", len(slice))
// Append elements
slice = append(slice, 4, 5)
fmt.Println("Updated slice length:", len(slice))
}
The array length remains constant while the slice length changes as elements
are added. Both use the same len function syntax.
Map length
The len function works with maps to count key-value pairs.
This example shows map length calculation before and after modifications.
package main
import "fmt"
func main() {
colors := map[string]string{
"red": "#FF0000",
"green": "#00FF00",
"blue": "#0000FF",
}
fmt.Println("Initial map length:", len(colors))
// Add a new entry
colors["white"] = "#FFFFFF"
fmt.Println("After addition:", len(colors))
// Delete an entry
delete(colors, "green")
fmt.Println("After deletion:", len(colors))
}
Map length changes as key-value pairs are added or removed. The len
function provides the current count of elements in the map.
Channel length
For channels, len returns the number of queued elements.
This example demonstrates channel length measurement during communication.
package main
import "fmt"
func main() {
ch := make(chan int, 3)
fmt.Println("Empty channel length:", len(ch))
ch <- 1
ch <- 2
fmt.Println("After two sends:", len(ch))
<-ch
fmt.Println("After one receive:", len(ch))
close(ch)
}
Channel length reflects the number of elements currently in the buffer. This can be useful for monitoring channel usage in concurrent programs.
Custom type with Len method
While len works with built-in types, we can implement our own
Len method for custom types. This example shows both approaches.
package main
import "fmt"
type Queue struct {
elements []int
}
func (q *Queue) Len() int {
return len(q.elements)
}
func main() {
q := Queue{elements: []int{1, 2, 3, 4}}
// Using custom Len method
fmt.Println("Queue length (method):", q.Len())
// Using built-in len on the slice
fmt.Println("Queue length (built-in):", len(q.elements))
}
The custom Len method provides a clean interface while internally
using the built-in len function. Both approaches give the same result.
Source
This tutorial covered the len function in Go with practical
examples for strings, arrays, slices, maps, and channels.
Author
List all Golang tutorials.