all: use GOROOT/bin/go instead of go everywhere

This is a non-test code version of CL 191517.

Using 'go' command in tests is confusing when using a different version
of Go (e.g., go1.13rc1). Use GOROOT/bin/go so that the same Go is used.

Change-Id: Id5937cfa6dd6d3164d5f774e3fc0fd15b7f613b5
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/191518
Run-TryBot: Hajime Hoshi <hajimehoshi@gmail.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/cmd/gobind/gobind_test.go b/cmd/gobind/gobind_test.go
index e3d1351..a9f7eeb 100644
--- a/cmd/gobind/gobind_test.go
+++ b/cmd/gobind/gobind_test.go
@@ -47,8 +47,7 @@
 	bin.Close()
 	defer os.Remove(bin.Name())
 	if runtime.GOOS != "android" {
-		gocmd := filepath.Join(runtime.GOROOT(), "bin", "go")
-		if out, err := exec.Command(gocmd, "build", "-o", bin.Name(), "golang.org/x/mobile/cmd/gobind").CombinedOutput(); err != nil {
+		if out, err := exec.Command(goBin(), "build", "-o", bin.Name(), "golang.org/x/mobile/cmd/gobind").CombinedOutput(); err != nil {
 			log.Fatalf("gobind build failed: %v: %s", err, out)
 		}
 		gobindBin = bin.Name()
@@ -110,8 +109,7 @@
 		t.Fatal(err)
 	}
 
-	gocmd := filepath.Join(runtime.GOROOT(), "bin", "go")
-	gopath, err := exec.Command(gocmd, "env", "GOPATH").Output()
+	gopath, err := exec.Command(goBin(), "env", "GOPATH").Output()
 	if err != nil {
 		t.Fatal(err)
 	}
diff --git a/cmd/gobind/main.go b/cmd/gobind/main.go
index 3c889d1..eec3c37 100644
--- a/cmd/gobind/main.go
+++ b/cmd/gobind/main.go
@@ -17,6 +17,7 @@
 	"os"
 	"os/exec"
 	"path/filepath"
+	"runtime"
 	"strings"
 
 	"golang.org/x/mobile/internal/importers"
@@ -46,6 +47,10 @@
 	os.Exit(exitStatus)
 }
 
+func goBin() string {
+	return filepath.Join(runtime.GOROOT(), "bin", "go")
+}
+
 func run() {
 	var langs []string
 	if *lang != "" {
@@ -104,7 +109,7 @@
 
 	// Determine GOPATH from go env GOPATH in case the default $HOME/go GOPATH
 	// is in effect.
-	if out, err := exec.Command("go", "env", "GOPATH").Output(); err != nil {
+	if out, err := exec.Command(goBin(), "env", "GOPATH").Output(); err != nil {
 		log.Fatal(err)
 	} else {
 		ctx.GOPATH = string(bytes.TrimSpace(out))
diff --git a/cmd/gomobile/build.go b/cmd/gomobile/build.go
index c2f40c1..cdc69aa 100644
--- a/cmd/gomobile/build.go
+++ b/cmd/gomobile/build.go
@@ -287,7 +287,7 @@
 
 func goCmd(subcmd string, srcs []string, env []string, args ...string) error {
 	cmd := exec.Command(
-		"go",
+		goBin(),
 		subcmd,
 	)
 	if len(ctx.BuildTags) > 0 {
diff --git a/cmd/gomobile/init.go b/cmd/gomobile/init.go
index 0ada746..f71e853 100644
--- a/cmd/gomobile/init.go
+++ b/cmd/gomobile/init.go
@@ -321,7 +321,7 @@
 	if val := os.Getenv(name); val != "" {
 		return val
 	}
-	val, err := exec.Command("go", "env", name).Output()
+	val, err := exec.Command(goBin(), "env", name).Output()
 	if err != nil {
 		panic(err) // the Go tool was tested to work earlier
 	}
@@ -338,7 +338,12 @@
 		if env != "" {
 			env += " "
 		}
-		printcmd("%s%s%s", dir, env, strings.Join(cmd.Args, " "))
+		args := make([]string, len(cmd.Args))
+		copy(args, cmd.Args)
+		if args[0] == goBin() {
+			args[0] = "go"
+		}
+		printcmd("%s%s%s", dir, env, strings.Join(args, " "))
 	}
 
 	buf := new(bytes.Buffer)
diff --git a/cmd/gomobile/main.go b/cmd/gomobile/main.go
index b40379f..a8af7cb 100644
--- a/cmd/gomobile/main.go
+++ b/cmd/gomobile/main.go
@@ -18,6 +18,8 @@
 	"log"
 	"os"
 	"os/exec"
+	"path/filepath"
+	"runtime"
 	"unicode"
 	"unicode/utf8"
 )
@@ -84,12 +86,12 @@
 	os.Exit(2)
 }
 
+func goBin() string {
+	return filepath.Join(runtime.GOROOT(), "bin", "go")
+}
+
 func determineGoVersion() error {
-	gobin, err := exec.LookPath("go")
-	if err != nil {
-		return errors.New("go not found")
-	}
-	goVersionOut, err = exec.Command(gobin, "version").CombinedOutput()
+	goVersionOut, err := exec.Command(goBin(), "version").CombinedOutput()
 	if err != nil {
 		return fmt.Errorf("'go version' failed: %v, %s", err, goVersionOut)
 	}
diff --git a/cmd/gomobile/version.go b/cmd/gomobile/version.go
index 8c09a44..ff23f5c 100644
--- a/cmd/gomobile/version.go
+++ b/cmd/gomobile/version.go
@@ -34,7 +34,7 @@
 			return "", err
 		}
 		bindir := filepath.Dir(bin)
-		cmd := exec.Command("go", "list", "-f", "{{.Stale}}", "golang.org/x/mobile/cmd/gomobile")
+		cmd := exec.Command(goBin(), "list", "-f", "{{.Stale}}", "golang.org/x/mobile/cmd/gomobile")
 		cmd.Env = append(os.Environ(), "GOBIN="+bindir)
 		out, err := cmd.CombinedOutput()
 		if err != nil {
@@ -64,7 +64,7 @@
 }
 
 func mobileRepoRevision() (rev string, err error) {
-	b, err := exec.Command("go", "list", "-f", "{{.Dir}}", "golang.org/x/mobile/app").CombinedOutput()
+	b, err := exec.Command(goBin(), "list", "-f", "{{.Dir}}", "golang.org/x/mobile/app").CombinedOutput()
 	if err != nil {
 		return "", fmt.Errorf("mobile repo not found: %v, %s", err, b)
 	}