Finding Length of the Pointer
The length of a pointer may be determined using the len() method in pointers. This built-in function returns the total number of items in the supplied pointer to an array, even if the specified pointer is null. This function is classified as builtin.
Syntax
func len(p Type) int
Here, p is the pointer.
Example
Let's go through an example to have a better understanding.
// How to find the length of the pointer to an array using the Go language
package main
import (
"fmt"
)
func main() {
var pointer1 [5]*int
var pointer2 [4]*string
var pointer3 [3]*float64
// Finding the length of Pointer Array
fmt.Println("Length of Pointer 1: ", len(pointer1))
fmt.Println("Length of Pointer 2: ", len(pointer2))
fmt.Println("Length of Pointer 3: ", len(pointer3))
}
Output:
Length of Pointer 1: 5
Length of Pointer 2: 4
Length of Pointer 3: 3
Frequently Asked Questions
What is Go?
Go, often known as Golang, is a Google-developed open-source, compiled, and statically typed computer language. It's designed to be simple, fast, readable, and efficient.
What is a pointer in Golang?
In the Go programming language or Golang, pointers are variables that retain the memory address of another variable. In Golang, pointers are also known as special variables.
Which function to use to find the length of a pointer in Go?
In the go programming language, we can use the len() function to find the length of the pointer.
Conclusion
In this blog, we extensively discussed finding the length of a pointer in go. We also briefly described what a pointer is, along with an example. We hope our blog enhances your knowledge of the pointers and how to find their length.
You can visit Go Archives by coding ninjas for more information on Go Language.
See Basics of C++ with Data Structure, DBMS, Operating System by Coding Ninjas, and keep practicing on our platform Coding Ninjas Studio.
If you think you are ready for the tech giants company, check out the mock test series on code studio.
You can also refer to our Guided Path on Coding Ninjas Studio to upskill yourself in domains like Data Structures and Algorithms, Competitive Programming, Aptitude, and many more!. You can also prepare for tech giants companies like Amazon, Microsoft, Uber, etc., by looking for the questions asked by them in recent interviews. If you want to prepare for placements, refer to the interview bundle. If you are nervous about your interviews, you can see interview experiences to get the ideas about these companies' questions.
Nevertheless, you may consider our premium courses to give your career an edge over others!
Do upvote our blogs if you find them helpful and engaging!
Happy Learning!