Search code examples
gogo-modules

Go Modules: finding out right pseudo-version (vX.Y.Z-<timestamp>-<commit>) of required package


I am trying out Go modules. My project requires the libarary golang.org/x/net/html, so I defined this go.mod file:

module github.com/patrickbucher/prettyprint

require golang.org/x/net/html

And wrote this demo program to check if the dependency gets loaded upon compilation:

package main

import (
        "fmt"
        "log"
        "os"

        "golang.org/x/net/html"
)

func main() {
        doc, err := html.Parse(os.Stdin)
        if err != nil {
                log.Fatal(err)
        }
        fmt.Println(doc)
}

When I run go build, I get this error message:

go: errors parsing go.mod:
~/prettyprint/go.mod:3: usage: require module/path v1.2.3

Obviously, I missed the version number. But which one to take? I stumbled an article called Takig Go Modules for a Spin, where I found an example of a go.mod file containing references to golang.org/x packages:

module github.com/davecheney/httpstat

require (
        github.com/fatih/color v1.5.0
        github.com/mattn/go-colorable v0.0.9
        github.com/mattn/go-isatty v0.0.3
        golang.org/x/net v0.0.0-20170922011244-0744d001aa84
        golang.org/x/sys v0.0.0-20170922123423-429f518978ab
        golang.org/x/text v0.0.0-20170915090833-1cbadb444a80
)

The author is using version strings like v0.0.0-20170922011244-0744d001aa84, consisting of the semver indication v0.0.0, a timestamp and something that looks like a git commit ID.

How do I figure out those version strings? I guess those golang.org/x packages will be versioned according to semantic versioning at some point, but to really trying out go mod, I need to figure out those now.


Solution

  • A version of the form v0.0.0-20180906233101-161cd47e91fd means that there are no tagged versions on the git repository. So go mod generates one based on the latest commit time and the prefix of the commit hash.

    To get a correct go.mod file start out using the following command (assuming go 1.11):

    go mod init yourmodulename
    

    Or create an empty go.mod file that just contains the following:

    module yourmodulename
    

    then run go mod tidy, this will find all dependencies, add the missing and remove the unused dependencies.