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.
go mod tidy
to have thego
command automatically determine the corresponding pseudo-version in this case. Other commands likego build
,go test
, etc. would also have recorded the version. Some more details in this answer.go mod tidy
has its uses, but sometimes people think they need to run it more often than strictly needed. In any event, still valid to do so. – Viviennevivify