Fix Go compiler errors: declared and not used, imported and not used

Fix declared and not used and imported and not used in Go: remove dead code, use the blank identifier for side-effect imports, and avoid := shadowing in branches so the outer variable is actually assigned.

Published

Updated

Read time 2 min read

Reviewed byDeepak Prasad

Fix Go compiler errors: declared and not used, imported and not used

Go rejects unused local variables and unused imports. That catches mistakes early and keeps builds fast. The blank identifier _ can import a package for side effects only, or discard a value you must read for effect, but shadowing with := inside if / else is a common reason a variable looks “used” but still errors—see the third example. For scope rules, see variable scope in Go.

Tested with Go 1.24 on Linux.


imported and not used

go
package main

import (
	"fmt"
	_ "encoding/json"
)

func main() {
	name := "GoLinuxCloud"
	age := 1
	fmt.Println("My name is:", name, "and my age is:", age)
}
Output

Remove encoding/json entirely if you do not need it, or keep _ when the package must register codecs or similar in init.


declared and not used on a variable

go
package main

import "fmt"

func main() {
	page := "GoLinuxCloud"
	author := "Anonymous Author"
	fmt.Println("Page is:", page)
	_ = author
}
Output

Prefer deleting author or printing it; _ = author is only for transitional code.


Shadowing with := inside branches

If you write boolStr := "True" inside an if or else block, that := introduces a new variable scoped only to that block. The outer boolStr you meant to print stays at its zero value and triggers declared and not used. Assign to the outer name with = instead:

go
package main

import "fmt"

func main() {
	var boolStr string
	if false {
		boolStr = "False"
	} else {
		boolStr = "True"
	}
	fmt.Println(boolStr)
}
Output

You should see True.


Summary

The messages declared and not used and imported and not used mean the compiler sees no read of a binding or package. Fix real unused code first; use _ imports only for documented side effects; in branches, use = on existing names so you do not accidentally declare a shadowed variable. An IDE such as VS Code with the Go extension flags many of these before go build (getting started with Go).


References


Frequently Asked Questions

1. What does declared and not used mean in Go?

The compiler found a local variable that is never read; Go rejects that because unused locals often hide bugs and waste work.

2. How do I silence imported and not used?

Remove the import, or rename it to _ "path" when you only need init side effects from that package.

3. When is _ = x acceptable for an unused variable?

Rarely—usually you should delete the binding or use the value; occasionally you keep a value only for API symmetry while documenting why.

4. Why does my variable stay zero after an if block?

A short variable declaration := inside the branch can declare a new inner variable that shadows the outer one; use = on the outer name instead.

5. Where is variable scope explained?

See variable scope in Go for block rules and shadowing.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …