github.com/april1989/origin-go-tools@v0.0.32/cmd/godoc/goroot.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  	"runtime"
    12  	"strings"
    13  )
    14  
    15  func findGOROOT() string {
    16  	if env := os.Getenv("GOROOT"); env != "" {
    17  		return filepath.Clean(env)
    18  	}
    19  	def := filepath.Clean(runtime.GOROOT())
    20  	if runtime.Compiler == "gccgo" {
    21  		// gccgo has no real GOROOT, and it certainly doesn't
    22  		// depend on the executable's location.
    23  		return def
    24  	}
    25  	out, err := exec.Command("go", "env", "GOROOT").Output()
    26  	if err != nil {
    27  		return def
    28  	}
    29  	return strings.TrimSpace(string(out))
    30  }