Ai
1 Star 0 Fork 0

idsutong/gqlgen

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
gopath.go 994 Bytes
一键复制 编辑 原始数据 按行查看 历史
Adam Scarr 提交于 2018-08-23 13:27 +08:00 . Validate gopath when running gqlgen
package gopath
import (
"fmt"
"go/build"
"path/filepath"
"strings"
)
var NotFound = fmt.Errorf("not on GOPATH")
// Contains returns true if the given directory is in the GOPATH
func Contains(dir string) bool {
_, err := Dir2Import(dir)
return err == nil
}
// Dir2Import takes an *absolute* path and returns a golang import path for the package, and returns an error if it isn't on the gopath
func Dir2Import(dir string) (string, error) {
dir = filepath.ToSlash(dir)
for _, gopath := range filepath.SplitList(build.Default.GOPATH) {
gopath = filepath.ToSlash(filepath.Join(gopath, "src"))
if len(gopath) < len(dir) && strings.EqualFold(gopath, dir[0:len(gopath)]) {
return dir[len(gopath)+1:], nil
}
}
return "", NotFound
}
// MustDir2Import takes an *absolute* path and returns a golang import path for the package, and panics if it isn't on the gopath
func MustDir2Import(dir string) string {
pkg, err := Dir2Import(dir)
if err != nil {
panic(err)
}
return pkg
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/idsutong/gqlgen.git
git@gitee.com:idsutong/gqlgen.git
idsutong
gqlgen
gqlgen
v0.7.2

搜索帮助