Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Numbers passed by reference

package main

import "fmt"

func main() {
	a := 1
	fmt.Printf("before %v\n", a)
	inc(&a)
	fmt.Printf("after %v\n", a)
}

func inc(val *int) {
	fmt.Printf("address of val in inc: %v\n", val)
	fmt.Printf("val in inc: %v\n", *val)
	*val++
	fmt.Printf("val in inc: %v\n", *val)
}
before 1
address of val in inc: 0xc0000140e0
val in inc: 1
val in inc: 2
after 2