How to instantiate Struct Pointer Address Operator in Golang?


In Golang, struct pointers are used to create a pointer to a struct, which allows for more efficient memory management and manipulation of struct fields. In this article, we will discuss how to instantiate a struct pointer using the address operator in Golang.

Step 1: Define the Struct

The first step in instantiating a struct pointer is to define the struct itself. For example, let's define a simple struct representing a person with a name and age −

type Person struct {
   name string
   age  int
}

Step 2: Create a Struct Pointer

Once the struct is defined, we can create a struct pointer using the address operator "&". The address operator returns the memory address of the variable, which is stored as a pointer. To create a pointer to a struct, we simply apply the address operator to the struct variable −

person := Person{name: "John Doe", age: 30}
personPointer := &person

In this example, we create a Person struct variable named "person" and then create a pointer to it named "personPointer" using the address operator "&". The pointer "personPointer" now points to the memory location of the "person" variable.

Step 3: Accessing Struct Fields via Pointer

Once we have a pointer to a struct, we can access the struct fields using the arrow operator "->". The arrow operator dereferences the pointer and allows us to access the struct fields directly −

fmt.Println((*personPointer).name)
fmt.Println(personPointer->age)

In this example, we use the arrow operator "->" to access the "name" and "age" fields of the "person" struct through the pointer "personPointer". The parentheses around "personPointer" in the first line are necessary because the "." operator has higher precedence than the "*" operator.

Step 4: Modifying Struct Fields via Pointer

Struct pointers are often used to modify struct fields directly in memory. To do this, we simply use the arrow operator "->" to access the field we want to modify and then assign a new value to it −

personPointer->age = 35

In this example, we modify the "age" field of the "person" struct by assigning a new value to it through the pointer "personPointer".

Example

package main

import "fmt"

type Person struct {
   name string
   age  int
}

func main() {
   // Step 1: Define the Struct
   type Person struct {
      name string
      age  int
   }

   // Step 2: Create a Struct Pointer
   person := Person{name: "John Doe", age: 30}
   personPointer := &person

   // Step 3: Accessing Struct Fields via Pointer
   fmt.Println((*personPointer).name)
   fmt.Println(personPointer.age)

   // Step 4: Modifying Struct Fields via Pointer
   personPointer.age = 35
   fmt.Println(person)
}

Output

John Doe
30
{John Doe 35}

Conclusion

In Golang, struct pointers are used to create a pointer to a struct, which allows for more efficient memory management and manipulation of struct fields. To instantiate a struct pointer, we use the address operator "&" to create a pointer to a struct variable. We can then access and modify the struct fields using the arrow operator "->". By following the steps outlined in this article, you can create and manipulate struct pointers in your Golang programs.

Updated on: 08-May-2023

499 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements