Use go/build to get default GOPATH

This commit is contained in:
Viktor Oreshkin
2018-09-12 02:57:09 +03:00
parent 60711f1a83
commit ca8ad92f71
2 changed files with 2 additions and 12 deletions
-7
View File
@@ -1,11 +1,9 @@
package parser
import (
"bytes"
"go/ast"
"go/parser"
"go/token"
"os/exec"
"strings"
)
@@ -91,8 +89,3 @@ func (p *Parser) Parse(fname string, isDir bool) error {
}
return nil
}
func getDefaultGoPath() (string, error) {
output, err := exec.Command("go", "env", "GOPATH").Output()
return string(bytes.TrimSpace(output)), err
}
+2 -5
View File
@@ -10,6 +10,7 @@ import (
"path/filepath"
"strconv"
"strings"
"go/build"
)
func getPkgPath(fname string, isDir bool) (string, error) {
@@ -137,11 +138,7 @@ func getModulePath(goModPath string) string {
func getPkgPathFromGOPATH(fname string, isDir bool) (string, error) {
gopath := os.Getenv("GOPATH")
if gopath == "" {
var err error
gopath, err = getDefaultGoPath()
if err != nil {
return "", fmt.Errorf("cannot determine GOPATH: %s", err)
}
gopath = build.Default.GOPATH
}
for _, p := range strings.Split(gopath, string(filepath.ListSeparator)) {