Introduction
A pointer is a variable that holds memory address of another variable.
They point where the memory is allocated and provide ways to find or even change the value located at the memory
Address and Dereference Operator
& operator - The address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as address-of operator.
- operator - It is known as the dereference operator. when placed before an address, it returns the value at that address.
main.go
package main
import "fmt"
func main() {
i := 10
fmt.Printf("%T %v \n", &i, &i)
fmt.Printf("%T %v \n", *(&i), *(&i))
}
>>> go run main.go
*int 0xc00018c008
int 10
Declaring and Initializing a Pointer
declare a pointer
var *
var ptr_i *int
var ptr_s *string
main.go
package main
import "fmt"
func main() {
var i *int
var s *string
fmt.Println(s)
}
>>> go run main.go
initializing a pointer
var * = &
i := 10
var ptr_i *int = &i
Dereferencing a Pointer
*
main.go
package main
import "fmt"
func main() {
s := "hello"
fmt.Printf("%T %v \n", s, s)
ps := &s
*ps = "world"
fmt.Printf("%T %v \n", s, s)
}
>>> go run main.go
string hello
string world
Passing by Value in Functions
Function is called by directly passing the value of the variable as an argument.
The parameter is copied into another location of your memory.
So, when accessing or modifying the variable within your function, only the copy is accessed or modified, and the original value is never modified.
All basic types (int, float, bool, string, array) are passed by value.
main.go
package main
import "fmt"
func modify(s string) {
s = "world"
}
func main() {
a := "hello"
fmt.Println(a)
modify(a)
fmt.Println(a)
}
>>> go run main.go
hello
hello
Passing by Reference in Functions
Go supports pointer, allowing you to pass references to values within your program.
In call by reference/pointer, the address of the variable is passed into the function call as the actual parameter.
All the operations in the function are performed in the value stored at the address of the actual parameters.
package main
import "fmt"
func modify(s string) {
*s = "world"
}
func main() {
a := "hello"
fmt.Println(a)
modify(&a)
fmt.Println(a)
}
>>> go run main.go
hello
world
- slices are passed by reference by default
package main
import "fmt"
func modify(s []int) {
s[0] = 100
}
func main() {
slice := []int{10, 20, 30}
fmt.Println(slice)
modify(slice)
fmt.Println(slice)
}
>>> go run main.go
[10 20 30]
[100 20 30]
- Maps as well are passed by reference by default
package main
import "fmt"
func modify(s []int) {
m["K"] = 75
}
func main() {
ascii_codes := make(map[string]int)
ascii_codes["A"] = 65
ascii_codes["F"] = 70
fmt.Println(ascii_codes)
modify(ascii_codes)
fmt.Println(ascii_codes)
}
>>> go run main.go
map[A:65 F:70]
map[A:65 F:70 K:75]
That's great if you have make till here you have covered basic of Golang Pointer.
If you liked what you read, do follow and any feedback for further improvement will be highly appreciated!
Thank you and Happy Learning!👏