Last modified: Jan 23, 2026 By Alexander Williams
Reassign Variables in Go Functions
Reassigning variables inside functions is a core Go skill. It changes a variable's value after its initial creation. This guide explains the methods and rules.
Understanding variable reassignment is key to writing clear and effective Go code. It helps you manage state and data flow within your program's logic.
Basic Variable Reassignment
The simplest way is using the assignment operator =. You declare a variable, then give it a new value later.
package main
import "fmt"
func main() {
// Declare and initialize a variable
count := 5
fmt.Println("Initial count:", count)
// Reassign the variable
count = 10
fmt.Println("Reassigned count:", count)
// Reassign again with a calculation
count = count + 3
fmt.Println("Final count:", count)
}
Initial count: 5
Reassigned count: 10
Final count: 13
This works because count is in the same scope. The type cannot change. An integer must stay an integer.
Reassigning Function Parameters
Function parameters are local copies. Reassigning them inside the function does not affect the original argument.
package main
import "fmt"
func modifyParam(x int) {
fmt.Println("Param at start:", x)
x = 99 // Reassign the local copy
fmt.Println("Param after reassign:", x)
}
func main() {
original := 42
modifyParam(original)
fmt.Println("Original after function call:", original) // Unchanged
}
Param at start: 42
Param after reassign: 99
Original after function call: 42
The change to x is confined to the function. The original variable in main remains 42. This is known as "pass-by-value."
Using Pointers to Modify Originals
To modify a variable from a different scope, use a pointer. A pointer holds the memory address of a value.
This allows a function to change the value at that address. It's a powerful tool for shared state.
package main
import "fmt"
func modifyViaPointer(ptr *int) {
fmt.Println("Value via pointer at start:", *ptr)
*ptr = 99 // Dereference and reassign the value at the address
fmt.Println("Value via pointer after reassign:", *ptr)
}
func main() {
original := 42
fmt.Println("Original before:", original)
modifyViaPointer(&original) // Pass the address
fmt.Println("Original after:", original) // Changed!
}
Original before: 42
Value via pointer at start: 42
Value via pointer after reassign: 99
Original after: 99
The *ptr = 99 line reassigns the value at the stored address. This changes the original variable in main. For a deeper dive, see our guide on Go Pointers with Variables: Simple Guide.
Reassigning Named Return Values
Go functions can have named return values. These act like pre-declared variables inside the function. You can reassign them directly.
package main
import "fmt"
func calculate(x, y int) (sum int, product int) {
// 'sum' and 'product' are already declared
sum = x + y // Reassigning the return variable 'sum'
product = x * y // Reassigning the return variable 'product'
// A plain 'return' will send the current values of sum & product
return
}
func main() {
s, p := calculate(5, 7)
fmt.Println("Sum:", s, "Product:", p)
}
Sum: 12 Product: 35
Named returns are initialized to their zero values. Reassigning them is clear and common. Be mindful of variable shadowing if you reuse these names inside the function.
Reassignment with Multiple Return Values
Functions often return multiple values. You can reassign several variables at once using tuple assignment.
package main
import "fmt"
func swap(a, b string) (string, string) {
return b, a
}
func main() {
first, second := "Hello", "World"
fmt.Println("Before:", first, second)
// Reassign both variables using the function's return
first, second = swap(first, second)
fmt.Println("After swap:", first, second)
// You can also reassign with other multi-value expressions
second, first = first, second // Swapping back directly
fmt.Println("After direct swap:", first, second)
}
Before: Hello World
After swap: World Hello
After direct swap: Hello World
This is efficient and idiomatic Go. All values on the right are evaluated before any reassignment on the left happens. To ignore a value during assignment, use the Go Blank Identifier _ in Assignments Guide.
Common Pitfalls and Best Practices
Reassignment is straightforward but has nuances. Avoid these common mistakes.
Type Consistency: You cannot change a variable's type. A string variable cannot be reassigned to an int.
Scope Matters: You can only reassign a variable within its declared scope. A variable inside an if block cannot be reassigned outside of it. Learn more about Go Variable Scope in for Loops Explained.
Clarity Over Cleverness: While you can chain reassignments (a = b = 5 is invalid in Go), keep code simple. Reassign for a clear reason, like updating state or calculating a new result.
Use temporary variables for intermediate calculations if it improves readability. This aligns with best practices for temporary variables.
Conclusion
Reassigning variables in Go functions is fundamental. Use the = operator for local changes. Use pointers to modify variables in outer scopes.
Leverage named returns and multiple assignments for clean code. Always remember the rules of type safety and scope.
Mastering reassignment leads to predictable and maintainable Go programs. Combine this knowledge with understanding pointers and scope to write robust functions.