- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the Length of Channel, Pointer, Slice, String and Map in Golang?
In Golang, there are several data types such as channels, pointers, slices, strings, and maps. Each of these data types has a different way of storing and accessing data, and it is essential to know the length or size of each type to manage the memory efficiently. In this article, we will discuss how to find the length of a channel, pointer, slice, string, and map in Golang.
Finding the Length of Channel in Golang
A channel is a data type used for communication between Goroutines. We can find the length of a channel in Golang by using the len() function.
Example
Here is an example −
package main import "fmt" func main() { c := make(chan int, 5) c <- 1 c <- 2 c <- 3 fmt.Println("The length of the channel is", len(c)) }
Output
The length of the channel is 3
Finding the Length of Pointer in Golang
A pointer is a variable that stores the memory address of another variable. We cannot find the length of a pointer in Golang because it only stores the address of a variable. However, we can find the size of the variable that the pointer is pointing to by using the unsafe.Sizeof() function.
Example
Here is an example −
package main import ( "fmt" "unsafe" ) func main() { var a int64 = 100 var b *int64 = &a fmt.Println("The size of a is", unsafe.Sizeof(a)) fmt.Println("The size of b is", unsafe.Sizeof(b)) }
Output
The size of a is 8 The size of b is 8
Finding the Length of Slice in Golang
A slice is a dynamic array in Golang. We can find the length of a slice in Golang by using the len() function.
Example
Here is an example −
package main import "fmt" func main() { s := []int{1, 2, 3, 4, 5} fmt.Println("The length of the slice is", len(s)) }
Output
The length of the slice is 5
Finding the Length of String in Golang
A string is a sequence of characters in Golang. We can find the length of a string in Golang by using the len() function.
Example
Here is an example −
package main import "fmt" func main() { s := "Hello, World!" fmt.Println("The length of the string is", len(s)) }
Output
The length of the string is 13
Finding the Length of Map in Golang
A map is a key-value pair data structure in Golang. We can find the length of a map in Golang by using the len() function.
Example
Here is an example −
package main import "fmt" func main() { m := map[string]int{ "apple": 1, "banana": 2, "orange": 3, } fmt.Println("The length of the map is", len(m)) }
Output
The length of the map is 3
Conclusion
Finding the length of different data types is essential in Golang programming. By using the len() function for channels, slices, strings, and maps, and the unsafe.Sizeof() function for pointers, we can easily manage the memory of our program.