Updated April 10, 2023
Introduction to Go Pointers
Pointer in go language is the way to store the address of any variable, whatever we use variable either it is a string, or it is a numeric value all these variables stored in a particular memory location and, these memory locations are not in a simple format which we cannot be able to remember. To keep track and access any variable, we use a pointer, a pointer concept that allows us to access the memory address of any type of variable, either it is a string or numeric variable. To use a pointer, we need to use *data type and assign the value of the string of int variable to the defined pointer variable; we need to use & operator before the variable to get its memory address.
Syntax:
Below is a very simple format for the pointer variable’s syntax; here, we are using * to define variable pointer and & operator to get the memory address of any variable. We have defined data_type as it allows us to mention the pointer variable if it is going to hold a string or integer.
varname_of_pointer_variable *data_type(It can be integer or string)
How Does Pointers Work in Go?
We can explain the working of the pointer in go language with the help of the diagram below; let us understand the diagram given below.
- In the below diagram, we have taken two variables, x and y.
- Here x contains the value 100, and y contains the memory address of the x.
- So basically, a pointer is also one type of variable which holds the address of another variable.
- It means a pointer is also placed on any particular memory address.
- Follow the below diagram for a better understanding.
Types of Pointers in Go Language
We can define the types of the pointer in the Go language on the basis of the data types of the parables of which pointers are holding the memory address.
Example #1 – Pointer without any Variable
Create a file with name pointers.go and paste the below command and run the command go run pointers.go and see the below out of the execution of the code. We can explain the below example in the following steps.
- We have taken a pointer ptr and the type of pointer we have taken as the int.
- We have not assigned any variable to this pointer ptr.
- Finally, we are printing the value of the pointer ptr, which gives the result as nil. This means if we do not assign any variable to the pointer, it will not contain nil as the value.
Please see the below example.
Code:
//Importing the main package for running the go lang
package main
import "fmt"
funcmain() {
varptr *int // A pointer without any variable address assigned to it
// Printing the output of the variable
fmt.Println("ptr value is = ", ptr)
}
Output:
Example #2 – Pointer Using String
Create a file with name pointers.go and paste the below command and run the command go run pointers.go and see the below out of the execution of the code. We can explain the below example in the following steps.
- In the code below, we have taken a variable with the name student.
- We have taken a ptrvariable; this variable will hold the memory address of the variable student.
- We assigned the variable student to the ptr *string using the operator &student, here&student will fetch the student variable’s address.
- Finally, when we are printing their value, the address and value of student variables are printed on the console.
Please see the below example.
Code:
//Importing the main package for running the go lang
package main
import "fmt"
funcmain() {
//Defining variables
var student string = "Ranjan"
//assigning the memory address of the student variable to the ptr , note here student variable is a string .And ptr will hold the memory address of the student
varptr *string =&student
fmt.Println("student =", student)
fmt.Println("ptr value is =", ptr)
}
Output:
Example #3 – Pointer Using Integer
Create a file with name pointers.go and paste the below command and run the command go run pointers.go and see the below out of the execution of the code. We can explain the below example in the following steps.
- In the code below, we have taken the variables a and b and assigned value 10 and 2000.
- We have taken a ptrvariable; this variable will hold the memory address of the variable a and b.
- We assigned the variable a to the ptr *int using the operator &a here&a will fetch the address of the variable and assigned it to the pptr.
- In the next step, assign the variable b to the ptr *int using the operator &b, here&b will fetch the b variable’s address and assign it to the ptr pointer.
- Finally, when we are printing their value, the address and value are printed on the console.
Please see the below example.
Code:
//Importing the main package for running the go lang
package main
//Here fmt allow us to print something on console so we are importing it
//Here fmt allow us to print something on console so we are importing it
import "fmt"
funcmain() {
//Defining variables
a, b := 10, 2000
//assigning the memory address of the a variable to the ptr , note here a variable is a integer .And ptr will hold the memory address of the a variable.
ptr := &a // This we are creating pointer to the a
fmt.Println(*ptr) // Here we are printing pointer value of the a
*ptr = 20 // setting value a by a pointer
fmt.Println(a) // Printing the value of a
ptr = &b // This we are creating pointer to the b
*ptr = *ptr / 31 // performing the division of poiter of b by 31
fmt.Println(b) // Printing the value of b
}
Output:
Conclusion
From this tutorial, we learned about the basic concept of the pointer, and we also learned about the various types of pointer we can define and use in the go languages; we understand the working of the pointer with the help of the diagram and with the help of some examples.
Recommended Article
We hope that this EDUCBA information on “Go Pointers” was beneficial to you. You can view EDUCBA’s recommended articles for more information.