Go FAQ

Installation Issues

What to Do If Go Installation Fails?

Installation failure is usually caused by a few common reasons. First, check if the system meets the minimum requirements - Linux needs a relatively recent kernel version, and Windows needs a 64-bit system. If using the tarball installation, make sure you downloaded the correct version (amd64, arm64, etc.).

If you encounter permission errors, use sudo on Linux or macOS to elevate privileges. Make sure the installation target directory exists and is writable:

# Create installation directory
sudo mkdir -p /usr/local
sudo tar -C /usr/local -xzf go1.21.5.linux-amd64.tar.gz

If problems persist, try re-downloading the installer - the file may have been corrupted during download. Verify the SHA256 checksum:

sha256sum go1.21.5.linux-amd64.tar.gz

How to Verify Go Is Installed Correctly?

Run the following commands to check if Go is installed correctly:

go version
go env GOROOT
go env GOPATH

If output shows something like "go1.21.5 linux/amd64", installation was successful. If it says command not found, check if the PATH environment variable includes Go's bin directory.

Development Issues

How to Organize Project Structure?

Modules (introduced in Go 1.11) are the recommended dependency management approach. Create a new project:

mkdir myproject
cd myproject
go mod init github.com/username/myproject

Recommended project structure:

myproject/
├── go.mod
├── go.sum
├── main.go
├── internal/
│   ├── handler/
│   └── service/
└── pkg/
    └── utils/
  • main.go: Program entry point
  • internal: Private code that cannot be imported externally
  • pkg: Public libraries that can be used externally

What's the Difference Between GOPATH and Go Modules?

GOPATH is the dependency management method used before Go 1.11, requiring code to be placed in the $GOPATH/src directory. Go Modules, introduced in Go 1.11, is a modern approach that allows creating projects in any location with dependency management through go.mod files.

Go Modules is recommended as it solves many limitations of GOPATH including version management and offline development. From Go 1.16, Go Modules became the default mode.

How to Handle Circular Imports?

Go does not allow circular imports between packages, which causes compilation errors. The solution is to refactor code to eliminate circular dependencies:

  • Move shared types to a separate package
  • Use interfaces to decouple dependencies
  • Merge code into one package

Check for circular imports:

go mod tidy

How to Detect Goroutine Leaks?

Goroutine leaks usually occur when channels are not properly closed or no one receives from sent channels. Use the pprof tool to detect:

# Enable pprof
import _ "net/http/pprof"

# Export analysis data
go tool pprof http://localhost:6060/debug/pprof/goroutine

Or use runtime.NumGoroutine() to periodically print the number of goroutines to locate where the leak occurs.

How to Debug?

VS Code users can install the Go extension and press F5 to start debugging. GoLand has built-in powerful debugging support. Command-line debugging uses delve:

# Install delve
go install github.com/go-delve/delve/cmd/dlv@latest

# Start debugging
dlv debug main.go

Performance Issues

How to Profile Performance?

Go has built-in pprof performance analysis tool. First, enable the profiling endpoint in code:

import (
    "net/http"
    _ "net/http/pprof"
)

func main() {
    go func() {
        http.ListenAndServe("localhost:6060", nil)
    }()
    // Main program logic
}

Then use pprof for analysis:

go tool pprof http://localhost:6060/debug/pprof/profile  # CPU profiling
go tool pprof http://localhost:6060/debug/pprof/heap     # Memory profiling

How to Optimize Memory Usage?

Use sync.Pool to reduce memory allocations:

var bufferPool = sync.Pool{
    New: func() interface{} {
        return make([]byte, 1024)
    },
}

func process() {
    buf := bufferPool.Get().([]byte)
    defer bufferPool.Put(buf)
    // Use buf
}

Use -ldflags to reduce binary size:

go build -ldflags="-s -w" main.go

How to Write High-Performance Code?

  • Avoid creating objects in loops; reuse instead
  • Use defer for resource cleanup, but note defer has performance overhead
  • Use strconv instead of fmt for type conversion
  • Use strings.Builder for string concatenation
  • Pre-allocate slice capacity: make([]int, 0, 100)

Deployment Issues

How to Compile for Different Platforms?

Use GOOS and GOARCH environment variables for cross-compilation:

# Linux
GOOS=linux GOARCH=amd64 go build -o myapp-linux

# macOS
GOOS=darwin GOARCH=amd64 go build -o myapp-macos

# Windows
GOOS=windows GOARCH=amd64 go build -o myapp.exe

How to Reduce Binary File Size?

Static compilation reduces dependencies:

CGO_ENABLED=0 go build -ldflags="-s -w" main.go

You can also use upx for compression:

upx -9 myapp

Docker Multi-Stage Build

Use multi-stage builds to reduce image size:

# Build stage
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o main .

# Run stage
FROM alpine
WORKDIR /app
COPY --from=builder /app/main .
CMD ["./main"]

Learning Issues

How to Learn Go?

Recommended learning path:

  1. Read Go official documentation: https://go.dev/doc/
  2. Complete the A Tour of Go interactive tutorial
  3. Read "The Go Programming Language"
  4. Practice: Try writing a small project in Go
  5. Read source code of excellent open-source projects

What Types of Applications Is Go Suitable for?

Go is particularly suitable for developing: microservices, command-line tools, network servers, distributed systems, cloud computing platforms, container orchestration tools (like Kubernetes, Docker), blockchain applications, etc.

Go is not suitable for applications requiring complex object-oriented features, or scenarios with extremely high startup speed requirements (consider Rust).

  • Kubernetes: Container orchestration platform
  • Docker: Container runtime
  • Prometheus: Monitoring system
  • etcd: Distributed key-value store
  • Hugo: Static site generator
  • Gin: Web framework
  • Cobra: Command-line tool framework