August 2019
Beginner to intermediate
798 pages
17h 2m
English
A function can take pointer parameters provided that its signature allows it. The Go code of ptrFun.go will illustrate the use of pointers as function parameters.
The first part of ptrFun.go is as follows:
package main
import (
"fmt"
)
func getPtr(v *float64) float64 {
return *v * *v
}
So, the getPtr() function accepts a pointer parameter that points to a float64 value.
The second part of the program is shown in the following Go code:
func main() {
x := 12.2
fmt.Println(getPtr(&x))
x = 12
fmt.Println(getPtr(&x))
}
The tricky part here is that you need to pass the address of the variable to the getPtr() function because it requires a pointer parameter, which can be done by putting an ampersand in front of ...
Read now
Unlock full access