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
Golang Convert String into Snake Case
How to delete or remove element from a Map?
Golang Get current Date and Time in EST, UTC and MST?
Sample program to create csv and write data
Golang String Concatenation
How to use for and foreach loop?
How to update content of a text file?
How to check if a map contains a key in Go?
Golang download image from given URL
How to import and alias package names?