Understanding the Error
Go is strict about unused imports. You'll see this error:
./main.go:5:2: imported and not used: "fmt"
./main.go:6:2: imported and not used: "github.com/gin-gonic/gin"This compilation error prevents code bloat and ensures clean imports.
Common Scenarios and Solutions
Scenario 1: Truly Unused Import
Problem code: ```go package main
import ( "fmt" "os" )
func main() { fmt.Println("Hello") // os is imported but never used } ```
Solution - Remove unused import: ```go package main
import "fmt"
func main() { fmt.Println("Hello") } ```
Scenario 2: Import Needed for Side Effects (Blank Import)
Some packages need to be imported for their initialization effects.
Problem code: ```go package main
import ( "database/sql" "github.com/lib/pq" // Error: imported and not used )
func main() { db, _ := sql.Open("postgres", "dsn") // pq driver won't be registered! } ```
Solution - Use blank identifier: ```go package main
import ( "database/sql" _ "github.com/lib/pq" // Blank import registers the driver )
func main() { db, _ := sql.Open("postgres", "dsn") // pq driver is now registered via init() } ```
Common packages requiring blank imports:
``go
import (
_ "github.com/go-sql-driver/mysql" // MySQL driver
_ "github.com/lib/pq" // PostgreSQL driver
_ "github.com/mattn/go-sqlite3" // SQLite driver
_ "google.golang.org/grpc/balancer/roundrobin" // gRPC balancer
)
Scenario 3: Importing for Type/Constant References
Problem code: ```go package main
import "myapp/models"
func main() { // Error: imported and not used "myapp/models" user := struct { Name string }{ Name: "Alice", } } ```
Solution - Use the imported types: ```go package main
import "myapp/models"
func main() { user := models.User{ Name: "Alice", } fmt.Println(user.Name) } ```
Or use a type reference only: ```go package main
import "myapp/models"
func process(u models.User) {}
func main() { var _ models.User // Reference type without using a value // But this still gives "declared and not used"! } ```
Better approach - actually use the type: ```go package main
import "myapp/models"
func main() { var user models.User user.Name = "Alice" fmt.Println(user.Name) } ```
Scenario 4: Dot Import for Convenience
Problem code: ```go package main
import ( "fmt" . "strings" // Dot import )
func main() { fmt.Println(HasPrefix("hello", "he")) // Uses strings.HasPrefix directly } ```
While dot imports work, they're discouraged. Better:
Recommended - Use explicit package name: ```go package main
import ( "fmt" "strings" )
func main() { fmt.Println(strings.HasPrefix("hello", "he")) } ```
Scenario 5: Test File Imports
Test files often have this issue during development.
Problem code: ```go package main
import ( "testing" "myapp/models" // Error during go build )
func TestSomething(t *testing.T) { // models not used yet } ```
Solution - Test files should use _test suffix:
File: main_test.go
```go
package main
import ( "testing" "myapp/models" )
func TestUserCreation(t *testing.T) { user := models.User{Name: "test"} if user.Name != "test" { t.Error("Name mismatch") } } ```
Build with tests:
``bash
go test -v ./...
Scenario 6: Aliased Imports
Problem code: ```go package main
import ( "fmt" myfmt "fmt" // Error: imported and not used )
func main() { fmt.Println("Hello") } ```
Solution - Use the alias consistently: ```go package main
import ( myfmt "fmt" )
func main() { myfmt.Println("Hello") } ```
IDE and Editor Solutions
VS Code (with Go extension)
Enable automatic import organization:
// settings.json
{
"go.buildOnSave": "off",
"go.lintOnSave": "package",
"go.formatTool": "goimports",
"editor.formatOnSave": true
}Install goimports:
``bash
go install golang.org/x/tools/cmd/goimports@latest
GoLand / IntelliJ
Enable Go -> Go Modules -> Optimize imports on save.
Vim (with vim-go)
let g:go_fmt_command = "goimports"Command-Line Tools
goimports - Automatic Import Management
```bash # Install go install golang.org/x/tools/cmd/goimports@latest
# Fix a file goimports -w main.go
# Fix all files in directory goimports -w .
# Show diff without writing goimports -d main.go ```
go fmt - Basic Formatting
go fmt ./...Module Import Issues
Sometimes the error is due to module path problems:
Issue: Wrong Module Path
```go // go.mod module github.com/myuser/myapp
// main.go import "myapp/models" // Error: cannot find package ```
Solution - Use full module path:
``go
import "github.com/myuser/myapp/models"
Issue: Local Package Not Found
main.go:4:2: package myapp/internal/models is not in GOROOTSolution - Use Go modules:
``bash
go mod init github.com/myuser/myapp
go mod tidy
Workflow Tips
Development Workflow
```bash # 1. Write code with imports # 2. Run goimports to auto-fix goimports -w .
# 3. Build to verify go build ./...
# 4. Run tests go test ./... ```
Pre-commit Hook
Create .git/hooks/pre-commit:
``bash
#!/bin/bash
goimports -w .
go vet ./...
Make executable:
``bash
chmod +x .git/hooks/pre-commit
Verification
```bash # Check for unused imports go vet ./...
# Auto-fix imports goimports -w .
# Build successfully go build ./... ```