How to pass pointer to function in golang?

Member

raul_reichert

by raul_reichert , in category: Golang , 14 days ago

How to pass pointer to function in golang?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by wiley , 14 days ago

@raul_reichert If you want to pass variable as argument to function you can use * symbol to specify pointer in golang, some example:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
package main

import "fmt"

func main() {

   val := 0

   increaseValue(&val)

   // 1
   fmt.Println(val)
}

func increaseValue(val *int) {
   *val++
}