How to Use Feature Flags in Go

Use the GODEBUG environment variable or //go:debug directives to toggle specific Go runtime behaviors and compatibility settings.

Go uses the GODEBUG environment variable or //go:debug directives to toggle specific runtime behaviors, effectively acting as feature flags for compatibility and experimental features. Set the GODEBUG variable to a comma-separated list of key=value pairs to enable or disable features like HTTP/2 or panic behavior at runtime.

export GODEBUG=http2client=0,panicnil=1

Alternatively, embed the settings directly in your source code using //go:debug comments before the package declaration to persist the configuration across builds.

//go:debug http2client=0
//go:debug panicnil=1
package main

You can also define these defaults in your go.mod or go.work file using a godebug block to apply settings to the entire module or workspace.

godebug (
	http2client=0
	panicnil=1
)