What's Actually Happening
Go cannot find or download a package. Build fails with "cannot find package" or "package is not in GOROOT".
The Error You'll See
```bash $ go build
main.go:4:2: cannot find package "github.com/gin-gonic/gin" in any of: /usr/local/go/src/github.com/gin-gonic/gin (from $GOROOT) /home/user/go/src/github.com/gin-gonic/gin (from $GOPATH) ```
Or:
go: github.com/gin-gonic/gin@latest: module github.com/gin-gonic/gin: Get "https://proxy.golang.org/": dial tcp: connection refusedWhy This Happens
- 1.Package not in go.mod
- 2.Module not downloaded
- 3.Wrong import path
- 4.Network/proxy issues
- 5.Private repository
- 6.GOPATH mode vs modules
Step 1: Check Go Module Status
go mod init myapp
cat go.mod
go mod tidy
go mod downloadStep 2: Check Import Path
# Verify import path is correct
go get github.com/gin-gonic/gin
go list -m github.com/gin-gonic/ginStep 3: Download Dependencies
go mod download
go mod tidy
go get ./...Step 4: Check Go Environment
go env
go env GOPATH
go env GOMODCACHE
go env GOPROXYStep 5: Configure Proxy
go env -w GOPROXY=https://proxy.golang.org,direct
go env -w GOPROXY=https://goproxy.cn,direct
go env -w GOPRIVATE=github.com/myorg/*Step 6: Handle Private Repositories
git config --global url."git@github.com:".insteadOf "https://github.com/"
go env -w GOPRIVATE=github.com/myorg/*
go get github.com/myorg/private-repoStep 7: Clear Module Cache
go clean -modcache
rm -rf $GOPATH/pkg/mod
go mod downloadStep 8: Check Vendor Directory
go mod vendor
go build -mod=vendorStep 9: Update Dependencies
go get -u ./...
go get -u=patch ./...
go mod tidyStep 10: Verify Build
go build ./...
go test ./...
go run main.goRelated Issues
- [Fix Golang Build Failed](/articles/fix-golang-build-failed)
- [Fix Golang Module Version Mismatch](/articles/fix-golang-module-version-mismatch)