Go mod subcommand to print mod name of pwd or repo

I would like to access the computed go module name for the package in the current working directory, as go mod init would generate were the package a dependency in another project.

A way to return the computed name would be useful for operating system level packaging metadata.

An example:

$ git clone https://github.com/davecheney/httpstat
$ go mod init
go: creating new go.mod: module github.com/davecheney/httpstat
go: copying requirements from Gopkg.lock

$ go mod graph
github.com/davecheney/httpstat github.com/fatih/color@v1.5.0
github.com/davecheney/httpstat github.com/mattn/go-colorable@v0.0.9
github.com/davecheney/httpstat github.com/mattn/go-isatty@v0.0.3
github.com/davecheney/httpstat golang.org/x/net@v0.0.0-20170922011244-0744d001aa84
github.com/davecheney/httpstat golang.org/x/sys@v0.0.0-20170922123423-429f518978ab
github.com/davecheney/httpstat golang.org/x/text@v0.0.0-20170915090833-1cbadb444a80

Hypothetical: print the name corresponding to the state of the local git clone:

$ go mod name
github.com/davecheney/httpstat v1.0.0-201807162018-9bf6e1b6ca81

Print the name corresponding to the state of the upstream repository:

$ go mod name github.com/davecheney/httpstat
github.com/davecheney/httpstat v1.0.0-201807162018-9bf6e1b6ca81

It would be useful to allow for specifying specific git refs (tags, commits, branches) in both the local git clone and remote upstream repositories.

In the case of a bare tarball extract (no local git clone exists) and a go.mod file exists, access the upstream repository to compute the name:

$ go mod name
github.com/davecheney/httpstat v1.0.0-201807162018-9bf6e1b6ca81

If bare tarball extract (no local git clone) and offline, emit the bare module name as per go.mod:

$ go mod name

Is there an existing way to access the computed module name, or would this be a reasonable feature request?

(edit: revise tarball extract use case)
(edit: revise to mention utility of specifying arbitrary git refs in local and remote repos)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.