Pointers in Go programming language or 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. The memory address is always found in hexadecimal format(starting with 0x like 0xFFAAF etc.).
Before we start there are two important operators which we will use in pointers i.e.
* Operator also termed as the dereferencing operator used to declare pointer variable and access the value stored in the address.
& operator termed as address operator used to returns the address of a variable or to access the address of a variable to a pointer.
Declaring a pointer:
var pointer_name *Data_Type
Example: Below is a pointer of type string which can store only the memory addresses of string variables.
var s *string
Initialization of Pointer: To do this you need to initialize a pointer with the memory address of another variable using the address operator as shown in the below example:
// normal variable declaration var a = 45 // Initialization of pointer s with // memory address of variable a var s *int = &a
Example: In the following example, using pointer we will access the value of the variable whose address is stored in pointer.
Address of the variable: 0xc00010a040 Address stored in the pointer variable: 0xc00010a040 Value of the Actual Variable: Geeks For Geeks Value of the Pointer variable: Geeks For Geeks
Explanation: Here (&) symbol is used to get the address of the variable storing the value as “Geeks for Geeks”. The address owned by this variable is the same which is stored in the pointer. Therefore, the output of &dummyVar and pointerVariable is the same. To access the value stored at the address, which is stored in pointer we user (*) symbol.
- How to declare a global variable in PHP?
- Pointer to a Struct in Golang
- Atomic Variable in Golang
- How to check pointer or interface is nil or not in Golang?
- How to find the length of the pointer in Golang?
- How to find the capacity of the pointer in Golang?
- How to Access Interface Fields in Golang?
- Golang Pointer to an Array as Function Argument
- reflect.Pointer() Function in Golang with Examples
- How to fetch an Integer variable as String in GoLang?
- Golang Program that uses func as Local Variable
- Different Ways to Find the Type of Variable in Golang
- Function that takes an interface type as value and pointer in Golang
- How to find the capacity of Channel, Pointer and Slice in Golang?
- How to Copy Struct Type Using Value and Pointer Reference in Golang?
- How to instantiate Struct Pointer Address Operator in Golang?
- Golang program that uses func with variable argument list
- Different Ways to Convert an Integer Variable to String in Golang
- How to find the Length of Channel, Pointer, Slice, String and Map in Golang?
- Converting a string variable into Boolean, Integer or Float type 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 email@example.com. 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.