forked from mitchellh/gox
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgo_modern.go
39 lines (30 loc) · 1 KB
/
go_modern.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//go:build go1.16
// +build go1.16
package main
import (
"fmt"
"os"
"path/filepath"
)
// GoVersion reads the version of `go` that is on the PATH. This is done
// instead of `runtime.Version()` because it is possible to run gox against
// another Go version.
func GoVersion() (string, error) {
// NOTE: We use `go run` instead of `go version` because the output
// of `go version` might change whereas the source is guaranteed to run
// for some time thanks to Go's compatibility guarantee.
td, err := os.MkdirTemp("", "gox")
if err != nil {
return "", err
}
defer os.RemoveAll(td)
// Write the source code for the program that will generate the version
sourcePath := filepath.Join(td, "version.go")
if err := os.WriteFile(sourcePath, []byte(versionSource), 0644); err != nil {
return "", err
}
// Execute and read the version, which will be the only thing on stdout.
version, err := execGo("go", nil, "", "run", sourcePath)
fmt.Printf("Detected Go Version: %s\n", version)
return version, err
}