2 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊
您可以像這樣與標(biāo)志和標(biāo)志一起使用:go list
-m
-f
go list -m -f '{{.Dir}}' example.com/b
旗幟:-m
導(dǎo)致 go list 列出模塊而不是包。在這種模式下,去列表的參數(shù)可以是模塊,模塊模式(包含...通配符)、版本查詢或特殊模式 all,它與構(gòu)建列表中的所有模塊匹配。如果未指定任何參數(shù),則列出主模塊。
(參考)
旗幟:-f
使用包模板的語(yǔ)法指定輸出的備用格式。使用標(biāo)志時(shí),傳遞給模板的結(jié)構(gòu)為:
-m
type Module struct {
Path string // module path
Version string // module version
Versions []string // available module versions (with -versions)
Replace *Module // replaced by this module
Time *time.Time // time version was created
Update *Module // available update, if any (with -u)
Main bool // is this the main module?
Indirect bool // is this module only an indirect dependency of main module?
Dir string // directory holding files for this module, if any
GoMod string // path to go.mod file for this module, if any
GoVersion string // go version used in module
Error *ModuleError // error loading module }
type ModuleError struct {
Err string // the error itself
}

TA貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以像這樣計(jì)算出模塊路徑:
package main
import (
"fmt"
"os"
"path"
"golang.org/x/mod/module"
)
func GetModulePath(name, version string) (string, error) {
// first we need GOMODCACHE
cache, ok := os.LookupEnv("GOMODCACHE")
if !ok {
cache = path.Join(os.Getenv("GOPATH"), "pkg", "mod")
}
// then we need to escape path
escapedPath, err := module.EscapePath(name)
if err != nil {
return "", err
}
// version also
escapedVersion, err := module.EscapeVersion(version)
if err != nil {
return "", err
}
return path.Join(cache, escapedPath+"@"+escapedVersion), nil
}
func main() {
var path, err = GetModulePath("github.com/jakubDoka/mlok", "v0.4.7")
if err != nil {
panic(err)
}
if _, err := os.Stat(path); os.IsNotExist(err) {
fmt.Println("you don't have this module/version installed")
}
fmt.Println("module found in", path)
}
- 2 回答
- 0 關(guān)注
- 169 瀏覽
添加回答
舉報(bào)