Table of contents
1.
Introduction
2.
Pointers in Go Programming
3.
Capacity of pointers in GoLang
4.
Frequently Asked Questions 
4.1.
What is the main advantage of GoLang?
4.2.
In Go, what is a pointer variable's default value?
4.3.
In Golang, what does a pointer do?
4.4.
In Go, how can we get the value of a pointer?
4.5.
In Golang, how big is a pointer?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Finding the Capacity of the Pointer in Go

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In the Go programming language, capacity refers to the maximum number of elements that can be contained in an array or any other element holding data type.

In this article, we’ll be looking at how to find the capacity of the Pointer in Go.

Before we jump into the method to find the capacity, let us first discuss what are pointers in Go?

Pointers in Go Programming

A pointer is a variable in the Go programming language or Golang which stores the memory address of other variables. In Golang, pointers are also called as special variables which are used to store data in the system at a specific memory address. 

The variable pointer is shown in the below figure.

Now that you have revised the pointers, let us now discuss the main section of this article i.e., Capacity of Pointers. 

Let’s start then: 

Capacity of pointers in GoLang

In the Go programming language, capacity refers to the maximum number of elements that can be contained in a pointer to an array. The cap() function which helps to find the capacity  of the pointer is a built-in function that returns the capacity of the pointer to an array. 

To know more about the pointers you can refer to the link size of pointers and returning pointers from a function in Go.

 

Syntax:

The cap() function in Golang takes an input and returns its capacity depending on the input type.

func cap(l Type) int

 

The following table describes the types of a single input I that the cap() function accepts. The return value is of type int.

I

Description

  array

The cap() function returns the number of elements in the array.

pointer to array

The cap() function returns the number of elements in the array.

slice

The cap() function returns the number of elements in the underlying array.

channel

The cap() function returns the channel buffer capacity in units of elements.

Example 1:

package main

import "fmt"

func main() {

	// creating an array of size 8 using make function

	val1 := make([]string, 0, 8)
	fmt.Printf("\nValue1: %d", cap(val1))
	val2 := make(chan string, 12)
	fmt.Printf("\nValue2: %d", cap(val2))

	// Creating Pointer

	var ptr *[15]string

	// Displaying the result using cap() function

	fmt.Printf("\nPointer: %d", cap(ptr))
}

 

 

Output:

Example 2:

package main

import (
	"fmt"
)

func main() {

	// Creating a slice using shorthand declaration

	array := [6]int{28, 391, 43, 55, 690, 480}

	var a int

	var b [5]*int
	// iterate using loop
	for a = 0; a < len(b); a++ {
		b[a] = &array[a]
	}
	for a = 0; a < len(b); a++ {

		fmt.Printf("Value of b[%d] = %d\n", a, *b[a])
	}
	// Displaying the result using cap() function

	fmt.Println("Capacity of arr: ", cap(array))
	fmt.Println("Capacity of p: ", cap(b))
}

 

Output:

Example 3 :

package main

import "fmt"

func main() {
	// declaring pointers
	var p1 [6]*int
	var p2 [4]*string
	var p3 [5]*float64

	// Displaying the result using cap() function

	fmt.Println("p1 capacity : ", cap(p1))
	fmt.Println("p2 capacity : ", cap(p2))
	fmt.Println("p3 capacity : ", cap(p3))
}

 

Output:

Frequently Asked Questions 

What is the main advantage of GoLang?

Go (also known as Golang or Go language) is an open-source general-purpose computer language. Google engineers created Go in order to construct stable and efficient software. Go is statically typed and explicit, it is most closely modelled after C language.

In Go, what is a pointer variable's default value?

The memory address of a value is stored in a pointer (variable). In the Go programming language, a pointer's default value is nil.

In Golang, what does a pointer do?

A pointer is a variable in the Go programming language or Golang that stores another variable's  memory address. They are the special variables that are used to store data in the system at a specific memory address.

In Go, how can we get the value of a pointer?

To determine the value (data) of a pointer, we must use the * operator, also known as the dereferencing operator, which, when placed before a pointer variable (like the & operator to obtain a memory address), returns the data in that memory.

In Golang, how big is a pointer?

A pointer is typically the same size as the architecture of your system, 32 bits on a 32 bit system and 64 bits on a 64 bit system.

Conclusion

To summarise this blog, we learned about pointers, their different types and capacity of pointers in GoLang. Further, we also discussed the three examples for proper understanding of the capacity of pointers.

To enhance your understanding you can refer to these articles: Returning Pointer from a Function in GoPointer to an Array as Function Argument in Go and many more. 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available, Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!
 

Live masterclass