Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Structs and pointers in Go Programming
3.
Pointer to Struct in Go
3.1.
Advantages of Using a Pointer to Struct
4.
Frequently Asked Questions 
4.1.
Why would you use a struct pointer?
4.2.
In Golang, how does struct work?
4.3.
In Golang, is struct a pointer?
4.4.
In Golang, how can I make a struct pointer?
4.5.
In Golang, when should we use pointers?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Pointer to Struct in Go

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Pointers in Golang are the special variables which are used to store the memory address of another variable. In Golang the * (asterisk) character is used to signify a pointer, which is followed by the type of the stored value. In the zero function ' xPtr' is an int pointer. The  *  symbols can also be used to "dereference" pointer variables. We can get the value that a pointer points to, by dereferencing it.

In Golang, a structure or struct is a user-defined type that allows elements of various types to be combined into a single type. A struct can be used to represent any real-world entity that has some set of properties. A pointer to a struct can also be used in Golang.

In this article, we are going to take a deeper look into the Pointer to Struct in Go.

 

Structs and pointers in Go Programming

In Golang, a struct is a user-defined type of language that allows elements of different types to be combined into a single type. 

The & operator, also known as the address operator, can be used to use a pointer to a struct. Golang allows programmers to use pointers to access the fields of a structure without explicitly dereferencing it. 

Pointers in Golang is a variable which is used to store the memory address of another variable. 

They are the special variables that are used to store some data at a particular memory address in the system. Pointers are also helpful for a different type of parameter passing which is usually referred as Pass By Address.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Pointer to Struct in Go

Golang allows programmers to use pointers to access the fields of a structure without explicitly dereferencing it. Structs can be considered as a template for creating a data record, like an employee or a student record or an e-commerce product. The keyword 'type' comes first, followed by a name for the new struct, and lastly the keyword struct. A set of data fields with names and types are given within the curly brackets.

For example:

//declaring struct
type identifier struct{
area data_type
parameter data_type
thickness data_type
}

In the above example we can see how the struct is declared where the data type can be int, float, char, double, etc.

Example 1:

package main
  
import "fmt"


 // declare the struct


type Student struct {
    Sname  string
    rollno int
}
  
func main() {
  
    std := Student{"Akhil", 1026}


  // assign the pointer


    pts := &std
  
    fmt.Println(pts)
  
    pts.Sname = "Abhay"
  
    fmt.Println(pts)
}

 

Output:

Example 2:

We employ pointers in the function parameters when the size of the data we're going to manipulate is much larger. It's a standard approach in many programming languages. We can do the same in Go.

We can simply pass a pointer to a struct as an argument to the function, and then manipulate the struct using the pointer.

package main
import (
    "fmt"
)
//declaring the struct 
type X struct{
    Y string
}
 
func change(a *X){
    a.Y = "Y"
}
 
//assign the pointer
func main() {
    a := X{"X"}
    fmt.Println(a)   
    change(&a)
    fmt.Println(a)   
}

 

Output:

In the above example we can see that the function also modifies the struct's initial value. This is a crucial characteristic of utilising pointers as arguments. Instead of altering it locally, the pointer resets it to its original value. 

Advantages of Using a Pointer to Struct

A pointer is a data type that stores the address of a memory location. When we manipulate something using its address, we do so in the most efficient way possible. We are modifying the original value rather than making a copy of it. As a result, we must also exercise caution. Even though it is a more efficient method, it is not free.

The struct is a technique to store and manipulate organised data. Instead of manipulating structs directly, we should try manipulating their pointers.

Frequently Asked Questions 

Why would you use a struct pointer?

Pointers are useful since they are easier to "move around." Instead of copying the entire structure each time, you can just keep it in memory and pass a pointer to it around.

In Golang, how does struct work?

In Golang, a structure or struct is a user-defined type that allows elements of potentially diverse types to be combined into a single type. A struct can be used to represent any real-world thing with a collection of properties or fields. This approach is frequently likened to object-oriented programming's classes.

In Golang, is struct a pointer?

In Golang, you can access structs with a pointer.

We utilised the struct type pointer to access struct members in the preceding example: ptr.name - returns the name member's value. ptr. age - returns the age member's value.

In Golang, how can I make a struct pointer?

The & operator, also known as the address operator, can be used to use a pointer to a struct. Golang allows programmers to use pointers to access the fields of a structure without explicitly dereferencing it.

In Golang, when should we use pointers?

Pointers are being used for efficiency because everything in Go is passed by value, so we can pass an address where data is housed instead of the data's value, to avoid mistakenly modifying data, and to access an actual value in another function rather than a copy of it when we wish to mutate it.

Conclusion

To summarise this blog, we learned about pointers, Pointer to Struct in Go, and further, we've looked upon some Faqs based on the same. 

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

 

You can also consider our Online Coding Courses such as the DSA in PythonC++ DSA CourseDSA in Java Course to give your career an edge over others.

Do upvote our blog to help other ninjas grow.

Happy Learning!
 

Previous article
Pointer to an Array as Function Argument in Go
Next article
Comparing Pointers in Go
Live masterclass