Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API to get the module name

Tags:

go

go-modules

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.

like image 453
bsr Avatar asked Nov 07 '18 03:11

bsr


People also ask

How do I retrieve the base name of a module?

To retrieve the base name of a module in the current process, use the GetModuleFileName function to retrieve the full module name and then use a function call such as strrchr (szmodulename, '\') to scan to the beginning of the base name within the module name string.

How do I get the base name of a psapi module?

If PSAPI_VERSION is 1, this function is defined as GetModuleBaseName in Psapi.h and exported in Psapi.lib and Psapi.dll as a wrapper that calls K32GetModuleBaseName. Programs that must run on earlier versions of Windows as well as Windows 7 and later versions should always call this function as GetModuleBaseName.

How do I change the API names in Zoho CRM?

The Zoho CRM generates an API name internally while creating a custom module, custom field, or related list label. Please note that you cannot alter the API Names for the default modules, fields, and related lists. You can change the API names only for custom modules, fields, and related lists.

How do I find the path of a module?

Retrieves the fully qualified path for the file that contains the specified module. The module must have been loaded by the current process. To locate the file for a module that was loaded by another process, use the GetModuleFileNameEx function. A handle to the loaded module whose path is being requested.


3 Answers

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

like image 63
ssemilla Avatar answered Sep 21 '22 19:09

ssemilla


Try this?

package main

import (
    "fmt"
    "io/ioutil"
    "os"

    modfile "golang.org/x/mod/modfile"
)

const (
    RED   = "\033[91m"
    RESET = "\033[0m"
)

func main() {
    modName := GetModuleName()
    fmt.Fprintf(os.Stdout, "modName=%+v\n", modName)
}

func exitf(beforeExitFunc func(), code int, format string, args ...interface{}) {
    beforeExitFunc()
    fmt.Fprintf(os.Stderr, RED+format+RESET, args...)
    os.Exit(code)
}

func GetModuleName() string {
    goModBytes, err := ioutil.ReadFile("go.mod")
    if err != nil {
        exitf(func() {}, 1, "%+v\n", err)
    }

    modName := modfile.ModulePath(goModBytes)
    fmt.Fprintf(os.Stdout, "modName=%+v\n", modName)

    return modName
}
like image 20
Mohamed Bana Avatar answered Sep 21 '22 19:09

Mohamed Bana


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.

like image 44
thepudds Avatar answered Sep 23 '22 19:09

thepudds