How to beat "declared and not used" in Go: a complete guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: "Aah, this error again!"



If you've ever written in Go, you know that feeling. You just wrote a few lines of code, run the compilation, and... the compiler throws a red line in your face:



./main.go:10:2: x declared and not used


Familiar? This is perhaps the most popular "headache" for Go beginners. On Stack Overflow, this question has gathered thousands of views, and for good reason. Go is a strict language. If you declare a variable but don't use it, the compiler will refuse to build the program. No "well, maybe it'll come in handy later."



But don't rush to tear your hair out! In this article, we'll break down 5 proven methods to beat this error. You'll learn how to properly handle variables, which tools can help avoid the problem, and how to turn this "annoying obstacle" into a useful habit.



Let's go!



Why is Go so strict?



Before fighting the error, let's understand its nature. Unlike Python or JavaScript, where an unused variable is just a warning, in Go it's a compilation error. Why?



  • Code cleanliness. Go was created as a language for industrial development. Unused variables are clutter that makes code harder to read and maintain.
  • Bug prevention. Often a variable that is "declared but not used" is a sign that you forgot to do something. The compiler catches these moments.
  • Fast compilation. Less clutter means faster builds.


Now that we know the enemy, let's see how to defeat it.



Method 1: Blank Identifier — your lifesaver



The simplest and most commonly used method is the blank identifier — the underscore character _. It tells the compiler: "I know this variable exists, but I don't need anything from it, just ignore it."



Example: you call a function that returns multiple values, but you only need one.



package main

import "fmt"

func getData() (int, string) { return 42, "secret answer"}

func main() { // We only need the number, ignore the string value, _ := getData() fmt.Println("Value:", value)}


Here _ accepts the string but doesn't use it. No error. It works!



Important: _ is not a "trash bin." It's a full-fledged variable that cannot be read. You cannot write fmt.Println(_) — that's also a compilation error.



When to use: when you are absolutely sure you don't need the return value. For example, when calling functions that return an error, but you are confident it won't occur (though this is a debatable practice).



Method 2: Just use the variable (the most obvious)



Sounds trivial, but often we declare a variable and forget to use it. Just add a line that "consumes" it.



package main

import "fmt"

func main() { name := "Vasya" // Error: name declared and not used

// Fix: fmt.Println("Hello,", name)}


But what if you only need the variable for some intermediate calculation and don't want to print it? Use _:



package main

func main() { result := calculateSomething() _ = result // Just "use" the variable, doing nothing}

func calculateSomething() int { return 100}


This is a legal method. But it looks like a crutch. It's better to rethink the logic.



Method 3: Debugging with linters



The best defense is a good offense. Instead of fixing errors after compilation, use linters

Blogs

Book Recommendations