API获取模块名称

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

If your starting point is a go.mod file and you are asking how to parse it, I would suggest starting with go mod edit -json, which outputs a particular go.mod file in JSON format. Here is the documentation:

https://golang.org/cmd/go/#hdr-Edit_go_mod_from_tools_or_scripts

Alternatively, you could use rogpeppe/go-internal/modfile, which is a go package that can parse a go.mod file, and which is used by rogpeppe/gohack and some other tools from the broader community.

Issue #28101 I think tracks adding a new API to the Go standard library to parse go.mod files.

Here is a snippet of the documentation for go mod edit -json:

The -json flag prints the final go.mod file in JSON format instead of writing it back to go.mod. The JSON output corresponds to these Go types:

type Module struct {
    Path string
    Version string
}

type GoMod struct {
    Module  Module
    Go      string
    Require []Require
    Exclude []Module
    Replace []Replace
}

type Require struct {
    Path string
    Version string
    Indirect bool
}

Here is an example snippet of JSON output from go mod edit -json that shows the actual module path (aka module name), which was your original question:

{
        "Module": {
                "Path": "rsc.io/quote"
        },

In this case, the module name is rsc.io/quote.