How to disable Golang unused import error

Go

Go Problem Overview


By default, Go treats unused import as error, forcing you to delete the import. I want to know if there exists some hope to change to this behavior, e.g. reducing it to warning.

I find this problem extremely annoying, preventing me from enjoying coding in Go.

For example, I was testing some code, disabling a segment/function. Some functions from a lib is no longer used (e.g. fmt, errors, whatever), but I will need to re-enable the function after a little testing. Now the program won't compile unless I remove those imports, and a few minutes later I need to re-import the lib.

I was doing this process again and again when developing a GAE program.

Go Solutions


Solution 1 - Go

Adding an underscore (_) before a package name will ignore the unused import error.

Here is an example of how you could use it:

import (
	"log"
	"database/sql"

	_ "github.com/go-sql-driver/mysql"
)

> To import a package solely for its side-effects (initialization), use > the blank identifier as explicit package name.

View more at https://golang.org/ref/spec#Import_declarations

Solution 2 - Go

The var _ = fmt.Printf trick is helpful here.

Solution 3 - Go

I have the same problem. I understand the reasoning for why they implemented the language to disallow unused imports and variables, but I personally find this feature annoying when writing my code. To get around this, I changed around my compiler to allow optional flags for allowing unused variables and imports in my code.

If you are interested, you can see this at https://github.com/dtnewman/modified_golang_compiler.

Now, I can simply run code with a command such as go run -gcflags '-unused_pkgs' test.go and it will not throw these "unused import" errors. If I leave out these flags, then it returns to the default of not allowing unused imports.

Doing this only required a few simple changes. Go purists will probably not be happy with these changes since there is good reason to not allow unused variables/imports, but I personally agree with you that this issue makes it much less enjoyable to code in Go which is why I made these changes to my compiler.

Solution 4 - Go

Use goimports. It's basically a fork of gofmt, written by Brad Fitzpatrick and now included in the go tools packages. You can configure your editor to run it whenever you save a file. You'll never have to worry about this problem again.

Solution 5 - Go

Use if false { ... } to comment out some code. The code inside the braces must be syntactically correct, but can be nonsense code otherwise.

Solution 6 - Go

If you are using the fmt package for general printing to console while you develop and test then you may find a better solution in the log package.

Solution 7 - Go

A lot of people have already commented with valid justification and I also acknowledge the original author's intention. However, Rob Pike mentioned in different forums that Go is the outcome of simplification of the processes that a few other mainstream programming languages either lack or not easy to achieve. It's Go's language semantics as well as to make the compilation faster, there are a lot of things that are adopted which initially seems inefficient.

To make it short, unused imports are considered as errors in Go as it blots the program and slows down the compilation. Using import for side effect (_) is a workaround, however, I find this confusing at times when there is a mix of valid imports with side effects along with side effects imported purely for the purpose of debugging/testing especially when the code base is large and there is a chance to forget and not delete unintentionally which may confuse other engineers/reviewers later. I used to comment out the unused ones, however, popular IDEs such as VS code and Goland can use goimports easily which does the insertion and deletion of the imports pretty well. Please refer to the link for more info, https://golang.org/doc/effective_go.html#blank_import

Solution 8 - Go

put this on top of your document and forget about unused imports:

import (
	"bufio"
	"fmt"
	"os"
	"path/filepath"
)

var _, _, _, _ = fmt.Println, bufio.NewReader, os.Open, filepath.IsAbs

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNickView Question on Stackoverflow
Solution 1 - GoAri SeyhunView Answer on Stackoverflow
Solution 2 - GoVolkerView Answer on Stackoverflow
Solution 3 - GodtzviView Answer on Stackoverflow
Solution 4 - GomdwhatcottView Answer on Stackoverflow
Solution 5 - GotopskipView Answer on Stackoverflow
Solution 6 - GoOldCurmudgeonView Answer on Stackoverflow
Solution 7 - GosbcharrView Answer on Stackoverflow
Solution 8 - GogoenView Answer on Stackoverflow