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 trim leading and trailing white spaces of a string in Golang?
Regular expression to validate phone number
How to fetch an Integer variable as String in Go?
Regular expression to extract numbers from a string in Golang
Print index and element or data from Array, Slice and Map
Subtract N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time.
How pointer & and * and ** works in Golang?
How To Make HTTP Requests in Go?
How to fix race condition using Atomic Functions in Golang?
How to find length of Map in Go?