Golang Passing Address to a Function
Passing the address of variable to the function and the value of a variables modified using dereferencing inside body of function.
Example
package main
import "fmt"
func update(a *int, t *string) {
*a = *a + 5 // defrencing pointer address
*t = *t + " Doe" // defrencing pointer address
return
}
func main() {
var age = 20
var text = "John"
fmt.Println("Before:", text, age)
update(&age, &text)
fmt.Println("After :", text, age)
}
Output
Before: John 20
After : John Doe 25
Most Helpful This Week
Golang Functions Returning Multiple Values
Creating a Function in Golang
Simple function with return value in Golang
Higher Order Functions in Golang
The return values of a function can be named in Golang
Anonymous Functions in Golang
What is Function in Golang
Naming Conventions for Golang Functions
User Defined Function Types in Golang
Simple function with parameters in Golang
Closures Functions in Golang