/errors/go/imported-and-not-used
Go error: "os" imported and not used
- tool
- Go
- exit code
- 1
- versions
- Go 1.20+ (message format), Go 1.x
- os
- any
"os" imported and not used is a Go compile error: the file imports a package and never uses it. Go treats unused imports as errors, not warnings, so the build stops. Delete the import, or let goimports / gopls manage imports on save. If the package is needed only for its init() side effects, such as a SQL driver, import it with the blank identifier: import _ "github.com/lib/pq".
Example
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println("hello")
}go build or go run . stops with the file, line and column of the import:
./main.go:5:2: "os" imported and not usedSince Go 1.20 the message puts the package path first; older releases printed imported and not used: "os". When the import has a name (import str "strings"), the message is "strings" imported as str and not used.
Fixes
import (
"fmt" // used
_ "github.com/lib/pq" // imported for its init() side effects only
)go install golang.org/x/tools/cmd/goimports@latest
goimports -w . # rewrite files: add missing, remove unused importsIn VS Code with the Go extension, gopls organizes imports on save by default for Go files.