Unused package imports
As you may have noticed, programs with unused package imports do not compile.
package main
import (
"fmt"
"log" // "imported and not used: log"
)
func main() {
fmt.Println("Hello")
}
../main.go:5:2: imported and not used: "log"
This is a deliberate feature of the Go language.
The presence of an unused variable may indicate a bug […] Go refuses to compile programs with unused variables or imports, trading short-term convenience for long-term build speed and program clarity. Go FAQ: Can I stop these complaints about my unused variable/import?
Workaround
There’s no compiler option to allow unused package imports. If you don’t want to remove/comment out the import, you can for instance use it in a dummy assignment:
package main
import (
"fmt"
"log"
)
var _ = log.Printf
func main() {
fmt.Println("Hello")
}
A better solution
Use the goimports tool, which rewrites a Go source file to have the correct imports. Many Go editors and IDEs run this tool automatically whenever a source file is written.
Further reading
Learn to love your compiler is a list of common Go compiler error messages: what they mean and how to fix the problem.