Passing parameter values

In Go, all parameters passed to a function are done so by value. This means a local copy of the passed values is created inside the called function. There is no inherent concept of passing parameter values by reference. The following code illustrates this mechanism by modifying the value of the passed parameter, val, inside the dbl function:

package main 
import ( 
func dbl(val float64) { 
   val = 2 * val // update param 
   fmt.Printf("dbl()=%.5f\n", val) 
func main() { 
   p := math.Pi 
   fmt.Printf("before dbl() p = %.5f\n", p) 
   fmt.Printf("after dbl() p = %.5f\n", p) 

When the program runs, it produces the following output that chronicles the state of the p variable before it is ...

Get Learning Go Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.