This section helps you diagnose and fix common issues in Go programs.
1. "undefined: X" Error
Problem
undefined: functionName
Causes
Solutions
// Check function exists
func functionName() { }
// Check package import
import "package/name"
// Check capitalization (exported functions start with capital)
func FunctionName() { } // Exported
func functionName() { } // Not exported
2. "cannot use X (type Y) as type Z" Error
Problem
cannot use x (type int) as type string
Causes
Solutions
// Explicit type conversion
var x int = 42
var s string = strconv.Itoa(x) // Convert int to string
// Check function signature
func process(s string) { }
process(strconv.Itoa(x)) // Correct
3. "imported and not used" Error
Problem
imported "package" and not used
Solutions
// Remove unused import
// Or use blank identifier
import _ "package" // Import for side effects only
4. "declared and not used" Error
Problem
x declared and not used
Solutions
// Use the variable
_ = x // Explicitly ignore
// Or remove if truly unused
5. "nil pointer dereference" Panic
Problem
panic: runtime error: invalid memory address or nil pointer dereference
Causes
Solutions
// Check for nil before use
if p != nil {
p.DoSomething()
}
// Initialize pointer
p := &MyStruct{}
// or
p := new(MyStruct)
6. "index out of range" Panic
Problem
panic: runtime error: index out of range [5] with length 3
Causes
Solutions
// Check bounds before access
if i < len(slice) {
value := slice[i]
}
// Use range for iteration
for i, v := range slice {
// Safe: i is always valid
}
7. "send on closed channel" Panic
Problem
panic: send on closed channel
Causes
Solutions
// Check if channel is closed
select {
case ch <- value:
// Sent successfully
case <-done:
// Channel closed or done signal
}
// Use recover for graceful handling
defer func() {
if r := recover(); r != nil {
// Handle panic
}
}()
1. Use Print Statements
fmt.Printf("Debug: value = %v\n", value)
fmt.Printf("Debug: at line %d\n", __LINE__) // Use runtime.Caller
Why print statements help
2. Use Delve Debugger
# Install Delve
go install github.com/go-delve/delve/cmd/dlv@latest
# Debug program
dlv debug ./main.go
# Common commands
break main.main # Set breakpoint
continue # Continue execution
next # Next line
print variable # Print variable value
Why Delve is powerful
3. Use go vet
go vet ./...
Why go vet helps
4. Use Race Detector
go test -race ./...
go run -race main.go
Why race detector is essential
5. Use Logging
import "log"
log.Printf("Processing item %d", i)
log.SetFlags(log.LstdFlags | log.Lshortfile)
Why logging helps
6. Use pprof for Profiling
go tool pprof http://localhost:6060/debug/pprof/profile
Why profiling helps
1. High Memory Usage
Symptoms
Diagnosis
go tool pprof http://localhost:6060/debug/pprof/heap
Solutions
sync.Pool for temporary objects2. Slow Execution
Symptoms
Diagnosis
go tool pprof http://localhost:6060/debug/pprof/profile
Solutions
3. Frequent GC Pauses
Symptoms
Diagnosis
import "runtime"
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Printf("GC runs: %d\n", m.NumGC)
Solutions
1. Race Conditions
Symptoms
Diagnosis
go test -race ./...
Solutions
2. Deadlocks
Symptoms
Diagnosis
import "runtime"
fmt.Printf("Goroutines: %d\n", runtime.NumGoroutine())
Solutions
3. Goroutine Leaks
Symptoms
Diagnosis
// Monitor goroutine count
for {
fmt.Printf("Goroutines: %d\n", runtime.NumGoroutine())
time.Sleep(1 * time.Second)
}
Solutions
4. Channel Leaks
Symptoms
Solutions
1. Debugging Crashes
# Get stack trace
go run main.go 2>&1 | tee crash.log
# Use GOTRACEBACK
GOTRACEBACK=all go run main.go
2. Debugging Performance
# CPU profile
go test -cpuprofile=cpu.prof ./...
go tool pprof cpu.prof
# Memory profile
go test -memprofile=mem.prof ./...
go tool pprof mem.prof
3. Debugging Concurrency
# Race detector
go test -race ./...
# Goroutine dump
kill -QUIT <pid> # Sends SIGQUIT, prints goroutine dump
4. Debugging Build Issues
# Verbose build
go build -v
# See commands
go build -x
# Check environment
go env
1. Read Error Messages
Go error messages are usually helpful. Read them carefully.
2. Check Documentation
go doc package
go doc package.Function
3. Search Go Issues
4. Use go vet and gofmt
go vet ./...
gofmt -d .
5. Minimal Reproducible Example
When asking for help, provide:
Troubleshooting is a skill that improves with experience. Use these tools and techniques to systematically diagnose and fix issues in your Go programs.