In Go, pointers allow you to store and work with the memory address of a variable, instead of the actual value. This is useful for passing large data structures efficiently or modifying a value inside a function.
To declare a pointer:
var ptr *intintmeans it's a pointer to anint.- A pointer stores the address of a value, not the value itself.
The & operator is used to get the address of a variable.
x := 42
ptr := &xNow ptr holds the memory address of x.
The * operator is used to access the value stored at a memory address (i.e., the value the pointer points to).
fmt.Println(*ptr) // prints 42fmt.Println("Welcome to Go Tutorial - Pointer!")
// Declare a pointer to an int (it holds the memory address of an int)
// Syntax - `var ptr * type`
var address *int
fmt.Println("Initially, the pointer has no value:", address) // nil
// Declare and initialize an integer
totalPosts := 56
fmt.Println("Total posts =>", totalPosts)
// Assign the address of totalPosts to the pointer
address = &totalPosts
fmt.Println("The address (memory location) of totalPosts =>", address)
// Dereferencing the pointer to get the value it points to
fmt.Println("The total post from pointer =>", *address)
// Demonstrating both pointer and dereferenced value together
fmt.Printf("The value stored at memory location %v is %v\n", address, *address)- Go does not support pointer arithmetic (like C/C++).
- You can use pointers to modify values inside a function.
- Default value of a pointer is
nilif not initialized.
In Go, functions receive copies of their arguments. If you want a function to modify the original value, you need to pass a pointer to that value.
By passing pointer:
- Avoids copying large structures
- Enables the function to change the original value
func updateLikes(likes *int) {
*likes += 1
}
func main() {
totalLikes := 100
fmt.Println("Before:", totalLikes)
// No need any return value or reassigning to another variable
// It update the value of memory location of `totalLikes`
updateLikes(&totalLikes)
fmt.Println("After:", totalLikes)
}
// Output
// Before: 100
// After: 101updateLikes(likes *int)takes a pointer to anint.&totalLikespasses the address oftotalLikes.- Inside the function,
*likesdereferences the pointer to access and modify the original value.
- Changes made via the pointer are reflected outside the function.
- Useful when dealing with large data structures or wanting to avoid value copies.