Common Anti-Patterns in Go and What to Do Instead

Fix Go anti-patterns by using GODEBUG settings in go.mod or environment variables to control compatibility behavior like panic(nil) and HTTP/2.

Avoid common Go anti-patterns like using panic(nil), ignoring GODEBUG settings, or misusing archive/tar formats by leveraging Go's built-in compatibility controls and strict type checking. Use //go:debug directives in your source files or godebug blocks in go.mod to explicitly control behavior changes like panicnil or http2client across toolchain versions. For example, to restore pre-Go 1.21 panic(nil) behavior in a Go 1.21+ project, add the following to your go.mod:

godebug (
	default=go1.21
	panicnil=1
)

Alternatively, set the environment variable GODEBUG=panicnil=1 before running your program to override defaults at runtime without changing source code.