blob: b3bb86efd529450bf27b1b0ce8f6bb33fb5b2f77 [file] [log] [blame]
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build !testmode
// +build !testmode
package internal
import (
"encoding/json"
"os/exec"
)
// GoEnv returns the value for key in `go env`. In
// the unlikely case that `go env` fails, prints an
// error message and returns an empty string.
func GoEnv(key string) string {
out, err := exec.Command("go", "env", "-json", key).Output()
if err != nil {
return ""
}
env := make(map[string]string)
if err := json.Unmarshal(out, &env); err != nil {
return ""
}
return env[key]
}