Pointers in Golang is a variable which is used to store the memory address of another variable. Pointers in Golang is also termed as the special variables. The variables are used to store some data at a particular memory address in the system.
You can also use a pointer to a struct. A struct in Golang is a user-defined type which allows to group/combine items of possibly different types into a single type. To use pointer to a struct, you can use & operator i.e. address operator. Golang allows the programmers to access the fields of a structure using the pointers without any dereferencing explicitly.
Example 1: Here, we are creating a structure named Employee which has two variables. In the main function, create the instance of the struct i.e. emp. After that, you can pass the address of the struct to the pointer which represents the pointer to struct concept. There is no need to use dereferencing explicitly as it will give the same result as you can see in the below program (two times ABC).
// Golang program to illustrate the // concept of the Pointer to struct package main import "fmt" // taking a structure type Employee struct { // taking variables name string empid int} // Main Function func main() { // creating the instance of the // Employee struct type emp := Employee{"ABC", 19078} // Here, it is the pointer to the struct pts := &emp; fmt.Println(pts) // accessing the struct fields(liem employee's name) // using a pointer but here we are not using // dereferencing explicitly fmt.Println(pts.name) // same as above by explicitly using // dereferencing concept // means the result will be the same fmt.Println((*pts).name) } |
Output:
&{ABC 19078}
ABC
ABC
Example 2: You can also modify the values of the structure members or structure literals by using the pointer as shown below:
// Golang program to illustrate the // concept of the Pointer to struct package main import "fmt" // taking a structure type Employee struct { // taking variables name string empid int} // Main Function func main() { // creating the instance of the // Employee struct type emp := Employee{"ABC", 19078} // Here, it is the pointer to the struct pts := &emp; // displaying the values fmt.Println(pts) // updating the value of name pts.name = "XYZ" fmt.Println(pts) } |
Output:
&{ABC 19078}
&{XYZ 19078}
Recommended Posts:
- How to create a Struct Instance Using a Struct Literal in Golang?
- How to instantiate Struct Pointer Address Operator in Golang?
- How to Copy Struct Type Using Value and Pointer Reference in Golang?
- Go Pointer to Pointer (Double Pointer)
- How to add a method to struct type in Golang?
- Printing Struct Variables in Golang
- How to find the type of Struct in Golang?
- How to use Field Tags in the Definition of Struct Type in Golang?
- How to instantiate Struct using new keyword in Golang?
- How to Compare Equality of Struct, Slice and Map in Golang?
- How to Assign Default Value for Struct Field in Golang?
- How to print struct variables data in Golang?
- Golang Program to Use Field Tags in the Definition of Struct Type
- Golang Pointer to an Array as Function Argument
- How to find the capacity of the pointer in Golang?
- How to find the length of the pointer in Golang?
- How to find the capacity of Channel, Pointer and Slice in Golang?
- reflect.Pointer() Function in Golang with Examples
- How to check pointer or interface is nil or not in Golang?
- How to declare and access pointer variable in Golang?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

