Coding⏱️ 2 min read📅 2026-05-31

How to Fix: How to avoid annoying error "declared and not used"

Learn how to avoid the 'declared and not used' error in Go.

Quick Answer: Use the -buildmode=partialfunc flag when compiling, or use a linter like golint to ignore unused imports.

To avoid the annoying error "declared and not used" in Go, you need to understand the purpose of this check. The compiler is ensuring that all variables and packages are used before they are declared.

⚠️ Common Causes

  • Declaring a new package without using it.
  • Uncommenting a command to test without using the declared variable or package.

🛠️ Step-by-Step Verified Fixes

Method 1: Disable the Check

  1. Step 1: Run the command `go build -v` or `go build -x` to enable verbose mode, which will show you the unused variables and packages.

Method 2: Use the `-trimpath` Flag

  1. Step 1: Run the command `go build -trimpath` to disable the check for unused packages.

💡 Conclusion

By understanding the purpose of this check and using one of the above methods, you can avoid the annoying error "declared and not used" in Go.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions