Skip to content

/errors/go/imported-and-not-used

Go error: "os" imported and not used

The commands on this page have not been executed by our verification runner yet.updated
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

main.go
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:

text
./main.go:5:2: "os" imported and not used

Since 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

go
import (
	"fmt"                    // used
	_ "github.com/lib/pq"   // imported for its init() side effects only
)
bash
go install golang.org/x/tools/cmd/goimports@latest
goimports -w .              # rewrite files: add missing, remove unused imports

In VS Code with the Go extension, gopls organizes imports on save by default for Go files.

Sources