Code with Go
Ask or search…
⌃K
Links

Pointers

This Go code demonstrates the concept of passing values by value and by reference (using pointers) in functions. Let's go through each part of the code with inline comments and additional explanations:
package main
​
import "fmt"
​
// zeroval takes an integer parameter by value and sets it to 0
func zeroval(ival int) {
ival = 0
}
​
// zeroptr takes a pointer to an integer as a parameter and sets the value it points to (dereferencing) to 0
func zeroptr(iptr *int) {
*iptr = 0
}
​
func main() {
// Declare and initialize an integer variable i with the value 1
i := 1
fmt.Println("initial:", i)
​
// Call zeroval with the value of i, but it won't change the original i
zeroval(i)
fmt.Println("zeroval:", i) // Output: zeroval: 1
​
// Call zeroptr with the address (pointer) of i, it will change the value of i through the pointer
zeroptr(&i)
fmt.Println("zeroptr:", i) // Output: zeroptr: 0
​
// Print the memory address of i using the & operator
fmt.Println("pointer:", &i)
}

Output

initial: 1
zeroval: 1
zeroptr: 0
pointer: 0xc00000a0b8
Explanation:
  1. 1.
    The zeroval function takes an integer ival as a parameter by value, meaning it receives a copy of the original value. The function sets the local copy of ival to 0, but this does not affect the original variable outside the function.
  2. 2.
    The zeroptr function takes a pointer to an integer iptr as a parameter. It dereferences the pointer using *iptr and sets the value it points to (in this case, the original variable i) to 0. This modification affects the original variable since it is passed by reference.
  3. 3.
    In the main function, an integer variable i is declared and initialized with the value 1.
  4. 4.
    The initial value of i is printed.
  5. 5.
    zeroval(i) is called, but it doesn't change the value of the original i because zeroval operates on a copy of the value.
  6. 6.
    The value of i is printed after calling zeroval, showing that the original value remains unchanged.
  7. 7.
    zeroptr(&i) is called, and the value of i is modified to 0 through the pointer.
  8. 8.
    The value of i is printed after calling zeroptr, demonstrating that the original variable has been modified.
  9. 9.
    The memory address of i is printed using the & operator, showing the location in memory where the variable is stored.