gobin.go raw

   1  package gobin
   2  
   3  import (
   4  	"errors"
   5  	"os"
   6  	"path/filepath"
   7  	"strings"
   8  )
   9  
  10  func Get() (gobinary string, e error) {
  11  	// search the environment variables for a GOROOT, if it exists we know we can run Go
  12  	env := os.Environ()
  13  	envMap := make(map[string]string)
  14  	for i := range env {
  15  		split := strings.Split(env[i], "=")
  16  		if len(split) < 2 {
  17  			continue
  18  		}
  19  		envMap[split[0]] = split[1]
  20  	}
  21  	goroot, ok := envMap["GOROOT"]
  22  	if ok {
  23  		gobinary = filepath.Join(goroot, "bin", "go")
  24  	} else {
  25  		e = errors.New("no GOROOT found, no Go binary available")
  26  	}
  27  	return
  28  }
  29