Nick
Nick

Reputation: 4552

How to disable Golang unused import error

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.

Upvotes: 146

Views: 50732

Answers (9)

yingshao xo
yingshao xo

Reputation: 385

I just made a new Golang compiler, which will simply ignore all variable unused warning and import unused warning and function unused warning by default.

https://github.com/yingshaoxo/go/releases/tag/v1.21

It works for both go run and go build

Upvotes: 2

sbcharr
sbcharr

Reputation: 21

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

Upvotes: 2

Michael Whatcott
Michael Whatcott

Reputation: 5985

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.

Upvotes: 22

goen
goen

Reputation: 71

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

Upvotes: 1

Ari Seyhun
Ari Seyhun

Reputation: 12531

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

Upvotes: 57

topskip
topskip

Reputation: 17375

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

Upvotes: 7

dtzvi
dtzvi

Reputation: 325

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.

Upvotes: 30

Volker
Volker

Reputation: 42458

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

Upvotes: 32

OldCurmudgeon
OldCurmudgeon

Reputation: 65859

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.

Upvotes: 4

Related Questions