How to Follow or Disable Redirects in Go

Web
Disable HTTP/2 redirects and other behaviors in Go by setting GODEBUG environment variables or using //go:debug directives.

You control redirects and HTTP/2 behavior using the GODEBUG environment variable or //go:debug directives in your source code. Set GODEBUG=http2client=0 to disable HTTP/2 for the client, or use //go:debug http2client=0 at the top of your main package file to compile this setting directly into the binary.

export GODEBUG=http2client=0,http2server=0
//go:debug http2client=0
package main

import "net/http"

func main() {
    // HTTP/2 is disabled for this client
    _ = http.Get("https://example.com")
}