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
Constructors in Golang
How to Draw a rectangle in Golang?
How to import and alias package names?
Get Year, Month, Day, Hour, Min and Second from current date and time.
How to get struct variable information using reflect package?
How to replace emoji characters in string using regex in Golang?
Closures Functions in Golang
Example to use Weekday and YearDay function
Select single argument from all arguments of variadic function
How to create Map using the make function in Go?