diff --git a/editor/formatter.go b/editor/formatter.go index 220a29e..a8031e0 100644 --- a/editor/formatter.go +++ b/editor/formatter.go @@ -19,8 +19,6 @@ import ( "net/http" "os" "os/exec" - "runtime" - "strings" "github.com/b3log/wide/conf" "github.com/b3log/wide/session" @@ -51,8 +49,7 @@ func GoFmtHandler(w http.ResponseWriter, r *http.Request) { filePath := args["file"].(string) - apiPath := runtime.GOROOT() + conf.PathSeparator + "src" + conf.PathSeparator + "pkg" - if strings.HasPrefix(filePath, apiPath) { // if it is Go API source code + if util.Go.IsAPI(filePath) { // ignore it return } diff --git a/file/files.go b/file/files.go index 5066852..4bacb02 100644 --- a/file/files.go +++ b/file/files.go @@ -21,7 +21,6 @@ import ( "net/http" "os" "path/filepath" - "runtime" "sort" "strings" @@ -56,10 +55,7 @@ var apiNode *FileNode // initAPINode builds the Go API file node. func initAPINode() { - apiPath := runtime.GOROOT() + conf.PathSeparator + "src" + conf.PathSeparator + "pkg" // before Go 1.4 - if !util.File.IsExist(apiPath) { - apiPath = runtime.GOROOT() + conf.PathSeparator + "src" // Go 1.4 and after - } + apiPath := util.Go.GetAPIPath() apiNode = &FileNode{Name: "Go API", Path: apiPath, IconSkin: "ico-ztree-dir-api ", Type: "d", Creatable: false, Removable: false, FileNodes: []*FileNode{}} diff --git a/util/go.go b/util/go.go new file mode 100644 index 0000000..79e9ce5 --- /dev/null +++ b/util/go.go @@ -0,0 +1,32 @@ +package util + +import ( + "path" + "runtime" + "strings" +) + +type mygo struct{} + +// Go utilities. +var Go = mygo{} + +// GetAPIPath gets the Go source code path. +// +// 1. before Go 1.4: $GOROOT/src/pkg +// 2. Go 1.4 and after: $GOROOT/src +func (*mygo) GetAPIPath() string { + ret := runtime.GOROOT() + "/src/pkg" // before Go 1.4 + if !File.IsExist(ret) { + ret = runtime.GOROOT() + "/src" // Go 1.4 and after + } + + return path.Clean(ret) +} + +// IsAPI determines whether the specified path belongs to Go API. +func (*mygo) IsAPI(path string) bool { + apiPath := Go.GetAPIPath() + + return strings.HasPrefix(path, apiPath) +}