Skip to content
... declared and not used

... declared and not used

DodaTech 3 min read

The “… declared and not used” error means you created a variable in your Go code but never referenced it. Go treats unused variables as a compile-time error to enforce clean, readable code and prevent subtle bugs caused by dead variables.

What It Means

Unlike many languages that only warn about unused variables, Go refuses to compile. Every declared local variable must be read at least once. This applies to variables declared with :=, var, and function parameters (though unused function parameters are allowed).

Why It Happens

  • You declared a variable but forgot to use it in subsequent logic.
  • You assigned a value to a variable but changed your approach and the old variable is now redundant.
  • You are using := short declaration and shadow an existing variable unintentionally.
  • You captured a return value from a function call but do not need all the values.
  • You are debugging or prototyping and left temporary variables in the code.

How to Fix It

1. Remove the unused variable

Delete the declaration entirely:

// Unused variable — causes error
func main() {
    count := 10
    fmt.Println("Hello")
}

// Fix: remove count
func main() {
    fmt.Println("Hello")
}

2. Use the blank identifier for unused return values

When a function returns multiple values and you only need some of them:

// err is unused — causes error
result, err := doSomething()

// Fix: use blank identifier for values you do not need
result, _ := doSomething()

3. Use the blank identifier to silence the error

If you need to keep a variable declaration for later, assign it to _:

count := getCount()
_ = count // silence the "declared and not used" error

This is useful during development when you plan to use the variable soon.

4. Combine declaration and usage

Reduce unnecessary intermediate variables:

// verbose — count is only used once
count := len(items)
fmt.Println(count)

// concise — no unused variable risk
fmt.Println(len(items))

5. Check for shadowed variables

A common mistake is declaring a new variable that shadows an existing one:

name := "Alice"
if true {
    name := "Bob" // new variable, outer name is unused
    fmt.Println(name)
}
// Outer "name" is never used after declaration

Fix by using assignment = instead of := inside the block.

FAQ

Why does Go reject unused variables while C and Python only warn?
Go’s philosophy is that unused variables indicate dead code, which can hide bugs and make refactoring harder. Enforcing this at compile time keeps the codebase clean and maintainable, even if it is stricter during development.
Does this error apply to package-level variables or function parameters?
Package-level variables and function parameters are exempt. Only local variables inside functions trigger this error. You can declare a package-level variable without using it anywhere.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro