Last modified: Jan 22, 2026 By Alexander Williams

Fix Common Go Variable Errors

Variables are the building blocks of any Go program. Yet, they are a common source of errors for beginners and experienced developers alike. Go's strict compiler and unique rules can lead to frustrating messages.

This guide walks you through the most frequent variable-related errors. You will learn what causes them and how to fix them quickly.

1. Unused Variable Error

Go enforces clean code. The compiler will fail if you declare a variable but never use it.


package main

func main() {
    var unusedMessage string = "Hello" // Error: unusedMessage declared and not used
    fmt.Println("Program runs")
}
    

The error is clear. You must use every variable you declare.

How to Fix:

Simply use the variable. Or, if it's for temporary testing, assign it to the blank identifier _.


package main

import "fmt"

func main() {
    var message string = "Hello"
    fmt.Println(message) // Variable is now used
}
    

For more on declaration syntax, see Go Variable Declaration: var vs := Explained.

2. No New Variables on Left Side of :=

The short declaration operator := is convenient. But it must declare at least one new variable in its scope.


package main

func main() {
    x := 10
    x := 20 // Error: no new variables on left side of :=
}
    

Here, x is already declared. Using := again is invalid.

How to Fix:

Use the standard assignment operator = to reassign an existing variable.


package main

import "fmt"

func main() {
    x := 10
    x = 20 // Correct: reassigns the value
    fmt.Println(x)
}
    

20
    

You can use := if you declare another new variable alongside the existing one.


y, x := 5, 20 // OK: y is new, x is reassigned
    

3. Variable Shadowing

Shadowing happens when a variable in an inner scope has the same name as one in an outer scope. This can cause logic bugs that are hard to spot.


package main

import "fmt"

func main() {
    count := 5
    if true {
        count := 10 // This shadows the outer 'count'
        fmt.Println("Inner count:", count)
    }
    fmt.Println("Outer count:", count) // Still 5, not 10
}
    

Inner count: 10
Outer count: 5
    

The inner count is a completely new variable. It does not modify the outer one.

How to Fix:

Avoid reusing variable names in nested scopes. Use distinct, descriptive names. If you intend to modify the outer variable, use assignment (=), not declaration (:=).


package main

import "fmt"

func main() {
    count := 5
    if true {
        count = 10 // Correctly modifies the outer variable
        fmt.Println("Modified count:", count)
    }
    fmt.Println("Outer count:", count)
}
    

Modified count: 10
Outer count: 10
    

4. Type Mismatch Errors

Go is statically typed. A variable's type cannot change after declaration. Assigning a value of the wrong type causes an error.


package main

func main() {
    var score int = 95
    score = "Excellent" // Error: cannot use "Excellent" (type string) as type int in assignment
}
    

How to Fix:

Ensure the assigned value matches the variable's declared type. If you need to change types, you must perform a type conversion (if valid) or use a new variable.


package main

import (
    "fmt"
    "strconv"
)

func main() {
    var score int = 95
    // Convert int to string for a message
    scoreMessage := "Score: " + strconv.Itoa(score)
    fmt.Println(scoreMessage)
}
    

Score: 95
    

Learn about safe practices in Go Type Conversion: Safe Variable Changes.

5. Using Variables Without Initialization

In Go, declared variables without an explicit value are given a zero value. This is not an error, but using an uninitialized variable from a function return can be.


package main

import "fmt"

func getNumber() (int, error) {
    return 42, nil
}

func main() {
    var result int // Zero value is 0
    // Wrong: We forgot to assign the return value
    getNumber()
    fmt.Println(result) // Prints 0, not 42
}
    

The program runs but has a logic error. The result variable was never assigned the function's return value.

How to Fix:

Always capture the return values of functions that provide them.


package main

import "fmt"

func getNumber() (int, error) {
    return 42, nil
}

func main() {
    result, err := getNumber() // Correctly assign both return values
    if err != nil {
        // handle error
    }
    fmt.Println(result)
}
    

42
    

For a full list of zero values, read Go Default Values for Uninitialized Variables.

6. Undeclared or Undefined Variable Error

This error occurs when you try to use a variable name that has not been declared in the current scope.


package main

import "fmt"

func main() {
    fmt.Println(userName) // Error: undefined: userName
}
    

How to Fix:

Declare the variable before you use it. Check for typos in the variable name.


package main

import "fmt"

func main() {
    userName := "Alice" // Variable is now declared
    fmt.Println(userName)
}
    

Alice
    

Conclusion

Most Go variable errors stem from the language's design priorities: simplicity, safety, and readability. The strict compiler is your friend. It catches problems early.

Remember these key points. Always use your variables. Understand the difference between := and =. Be mindful of variable scope and shadowing. Respect Go's static type system.

Fixing these common errors will make you a more proficient Go developer. Your code will be cleaner and more robust. Use tools like go vet to help catch these issues automatically.