How to Use Feature Flags in Go Production Environments

Use the GODEBUG environment variable or //go:debug directives to toggle specific Go runtime behaviors in production without recompiling.

Use the GODEBUG environment variable or //go:debug directives to toggle specific runtime behaviors in Go production environments. Set the environment variable before running your binary to enable or disable features like HTTP/2 or panic handling without recompiling.

export GODEBUG=http2client=0,panicnil=1
./my-production-binary

Alternatively, embed the setting directly in your source code using //go:debug directives at the top of your main package file:

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

import "fmt"

func main() {
	fmt.Println("Running with custom GODEBUG settings")
}

You can verify the active settings for a package using go list -f '{{.DefaultGODEBUG}}' ./my/package.