The cross-platform solution would be this, using a small compiled Go program:
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintln(os.Stderr, "usage: checkenv VARNAME")
os.Exit(2)
}
name := os.Args[1]
out, err := exec.Command("go", "env", "-json").Output()
if err != nil {
fmt.Fprintln(os.Stderr, "error running go env:", err)
os.Exit(1)
}
var vars map[string]string
if err := json.Unmarshal(out, &vars); err != nil {
fmt.Fprintln(os.Stderr, "error parsing json:", err)
os.Exit(1)
}
val, ok := vars[name]
if !ok {
fmt.Fprintf(os.Stderr, "ERROR - unknown go command variable %s\n", name)
os.Exit(1)
}
fmt.Printf("OK - Value of %s is %s:", name, val)
}
The advantage is that it runs identically on Windows and Linux, with no external dependencies (no jq, no python) — just Go, which is already installed anyway. You compile it once and use the binary from then on.
Name the file checkenv.go. The compiled binary will be checkenv (on Windows, checkenv.exe).
You can run it without compiling (for testing):
go run checkenv.go VARNAME
Or compiled, for repeated use:
go build -o checkenv checkenv.go
Then move the binary (checkenv or checkenv.exe) into a folder that’s in your PATH — for example ~/go/bin on Linux/macOS, or any folder you’ve manually added to PATH on Windows.
From there you can run it from anywhere, directly:
checkenv VARNAME
Examples :
checkenv GOPATH
OK - Value of GOPATH is /home/user/go:
checkenv GOTEST
ERROR - unknown go command variable GOTEST
If you run the command with no arguments or with more than one:
checkenv
usage: checkenv VARNAME
The messages (“ERROR - unknown go command variable X” and “OK - Value of X is Y”) can be changed freely to whatever wording is preferred.