Find length of Channel, Pointer, Slice, String and Map


In below program len function is used to find the length of a channel, pointer, slice, string and map.

Example

package main

import "fmt"

func main() {
	channelEx := make(chan int, 5)
	fmt.Printf("\nChannel: %d", len(channelEx))
	channelEx <- 0
	channelEx <- 1
	channelEx <- 2
	fmt.Printf("\nChannel: %d", len(channelEx))

	var pointerEx *[5]string
	fmt.Printf("\nPointer: %d", len(pointerEx))
	
	mapEx := make(map[string]int)
	mapEx["A"] = 10
	mapEx["B"] = 20
	mapEx["C"] = 30
	fmt.Printf("\nMap: %d", len(mapEx))
	
	sliceEx := make([]int, 10)
	fmt.Printf("\nSlice: %d", len(sliceEx))

	strEx := "Australia Canada Japan"
	fmt.Printf("\nString: %d", len(strEx))
}
Most Helpful This Week