100daysofcode - Day 12
Day 12 is already here ![]()
. Hope you are doing well and enjoying your journeys to the maximum. So guys, it’s time to continue and dive more into some interesting topics in Golang, to wrap up the day successfully. ![]()
![]()
In today’s post, we will talk about pointers in Go, their role and where to use them. Moving from some theoretical definitions to an interactive funny
example that illustrate the targeted topics in a clear way ![]()
![]()
What is a pointer in Golang ?
-
A pointer is simply a normal
variable that stores the address of a certain object stored in the memory. -
A pointer points to different variables with different data types like
int,string,booland more and only stores their memory address in hexadecimal
format. 
-
In Golang, pointers are called special
variables.
Why do we use pointers in Go ?
- Pointers are used when we need to pass
a variable by reference to a function or a method. 
- Pointers can be used to point
out an array, astruct, aninterface… in Golang. - Golang pointers avoid
unsafe
operations like updating or manipulating a value.
Pass by reference
Pass by value
- When we pass by value, a copy of the variable gets passed to the function to be used. This copy will be updated as required, but the original will be untouched.

- On the other hand
, while passing a variable by reference, this means that we are passing the address
of the original variable and any operation will affect the original data stored in the variable. 
What is a dereference operator in Golang pointer ?
- ﹡operator: the
*is used to declare a
pointer in Go, we can use it also to access the data value from the address where the pointer is pointing. 
What is the address
operator in Golang ?
- & operator: the
&is used to get the address
of the variable
How to declare a pointer
in Golang
?
var pointerName *TypepointerName: the name of the pointer nameType: the variable data type to where the pointer is pointing.- Example of
intandstringpointer:
package main
import "fmt"
func main() {
// A variable of type int
var integerNumber int
integerNumber = 34 // A number of type int
// A pointer of type string
var stringPointer *string
// A pointer of type int
var integerPointer *int
// integerPointer pointing to integerNumber
integerPointer = &integerNumber
fmt.Println("The address on which the pointer is pointing", integerPointer)
// Output: output in hex is 0x1400001c070
fmt.Println("The value of the variable to which the pointer is pointing", *integerPointer)
// Output: The value is: 34
// To change the value of the variable via the pointer we use:
*integerPointer = 45
fmt.Println("The new value of the variable integerNumber is: ", integerNumber)
// Output: 45
}
Create a pointer using the
keyword
package main
import "fmt"
func main() {
// create a pointer using new()
var newPointer = new(string)
*newPointer = "elie hannouch"
fmt.Println(newPointer) // 0x14000010230
fmt.Println(*newPointer) // elie hannouch
}
Functions with pointers
package main
import "fmt"
func updateName(name *string) {
// Using the dereference operator we change
// value of the name variable
*name = "Elie Hannouch"
}
func main() {
// Name with initial value
var name string = "John Doe"
fmt.Println("The original name is", name) // John Doe
// Passing the address of the name variable to the updateName function
updateName(&name)
fmt.Println("The original name is now changed to", name) // Elie Hannouch
}
- Stay tuned for the next posts , where we will talk about pointers with
structs,methodsand more
…