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
How to verify a string only contains letters, numbers, underscores, and dashes in Golang?
Sierpinski triangle in Go Programming Language
Find capacity of Channel, Pointer and Slice
How to get Dimensions of an image type jpg jpeg png or gif ?
How to check if a string contains only letters in Golang?
How to check string contains uppercase lowercase character in Golang?