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.


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


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