bin.go 512 B

123456789101112131415161718192021222324252627
  1. package golang
  2. import (
  3. "go/build"
  4. "os"
  5. "path/filepath"
  6. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  7. )
  8. // GoBin returns a path of GOBIN.
  9. func GoBin() string {
  10. def := build.Default
  11. goroot := os.Getenv("GOPATH")
  12. bin := filepath.Join(goroot, "bin")
  13. if !pathx.FileExists(bin) {
  14. gopath := os.Getenv("GOROOT")
  15. bin = filepath.Join(gopath, "bin")
  16. }
  17. if !pathx.FileExists(bin) {
  18. bin = os.Getenv("GOBIN")
  19. }
  20. if !pathx.FileExists(bin) {
  21. bin = filepath.Join(def.GOPATH, "bin")
  22. }
  23. return bin
  24. }