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: 0xc00000a0b8Explanation:
The
zerovalfunction takes an integerivalas a parameter by value, meaning it receives a copy of the original value. The function sets the local copy ofivalto 0, but this does not affect the original variable outside the function.The
zeroptrfunction takes a pointer to an integeriptras a parameter. It dereferences the pointer using*iptrand sets the value it points to (in this case, the original variablei) to 0. This modification affects the original variable since it is passed by reference.In the
mainfunction, an integer variableiis declared and initialized with the value 1.The initial value of
iis printed.zeroval(i)is called, but it doesn't change the value of the originalibecausezerovaloperates on a copy of the value.The value of
iis printed after callingzeroval, showing that the original value remains unchanged.zeroptr(&i)is called, and the value ofiis modified to 0 through the pointer.The value of
iis printed after callingzeroptr, demonstrating that the original variable has been modified.The memory address of
iis printed using the&operator, showing the location in memory where the variable is stored.
Last updated
Was this helpful?