Is there an API to get the module name of a project which uses go 1.11 module system?
so I need to get abc.com/a/m
from the module definition module abc.com/a/m
in go.mod
file.
Is there an API to get the module name of a project which uses go 1.11 module system?
so I need to get abc.com/a/m
from the module definition module abc.com/a/m
in go.mod
file.
As of this writing, I am not aware of any exposed APIs for that. However, looking at go mod
sources, there is a function that can be quite useful in Go mod source file
// ModulePath returns the module path from the gomod file text.
// If it cannot find a module path, it returns an empty string.
// It is tolerant of unrelated problems in the go.mod file.
func ModulePath(mod []byte) string {
//...
}
func main() {
src := `
module github.com/you/hello
require rsc.io/quote v1.5.2
`
mod := ModulePath([]byte(src))
fmt.Println(mod)
}
Which outputs github.com/you/hello