Skip to content

proposal: cmd/go: allow go run to pass custom environment variables  #63250

@dolmen

Description

@dolmen

This is a proposal to extend the syntax of the command line of go run to allow to pass environment variables to the program that is executed after its compilation.

The current syntax:

go run [build flags] [-exec xprog] package [arguments...]

The proposed syntax:

go run [build flags] [-exec xprog] [name=value ...] package [arguments...]

That extended syntax will be incompatible with = being used in the package (package name, .go file) specification. However the language reference already mentions that a compiler implementation may exclude = from import paths.

Why?

  1. It is useful to be able to pass to the compiled program some environment variables with values different from the values passed to go run which might be needed for building the program (example: x/text/cmd/gotext: generate for one GOOS/GOARCH from a different GOOS/GOARCH #37846)
  2. go run is often used in //go:generate commands. It is often needed to pass environment variables to the program, but there is no built-in portable way for calling a command with environment variables (env(1) is available on Unixes, but not on Windows. Workaround: github.com/dolmen-go/env)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions