【发布时间】:2019-10-31 04:39:31
【问题描述】:
我使用此代码获取导入到单个 Go 源文件中的依赖项列表:
// GetFileImports returns all the imports from the Golang source code file.
func GetFileImports(filepath string) ([]string, error) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, filepath, nil, parser.ImportsOnly)
if err != nil {
return nil, err
}
imports := make([]string, len(file.Imports))
for i := range file.Imports {
imports[i] = strings.Trim(file.Imports[i].Path.Value, "\"")
}
return imports, nil
}
我得到了这份清单:
namoled-core/data
namoled-core/shared
encoding/json
fmt
io/ioutil
log
net/http
github.com/gorilla/mux
github.com/gorilla/websocket
其中namoled-core/data 和namoled-core/shared 是我自己项目的一部分,github.com/gorilla/mux 和github.com/gorilla/websocket 是可下载的依赖项,其余都是标准库依赖项。是否有一种可靠且明确的方法来区分当前项目的依赖项、可下载的依赖项和标准库依赖项,仅通过它们的导入路径?考虑到项目路径也可能是 Github 链接。
【问题讨论】:
-
一般来说,导入路径是不够的,因为
go.mod可能包含replace指令,并且您模块中的任何包都可以映射到任何其他包,反之亦然(例如github.com/gorilla/mux可以替换为namoled-core/mux)。 -
“我自己的项目的一部分”也是一个只存在于人类的概念; Go 本身没有“项目”的概念。
-
@Adrian 是的,我知道,但是像 dep 和 Glide 这样的东西以某种方式确定了哪些依赖项是可下载的并且必须添加到索引文件中。
标签: go go-modules gopath go-get