go modules
Background
The golang 1.11 was released. This version implemented new feature “go modules”. This tool can lock dependent library version. This is similar feature as ruby bundler.
How to use
At first Initialize go.mod file.
$ go mod init your_module_name
Then go.mod file is generated.
$ ls -l
go.mod
The go.mod is defined module.
$ cat go.mod
module your_module_name
Next write test code as scrape.go.
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
)
func main() {
doc, err := goquery.NewDocument("https://blog.byplayer.org/")
if err != nil {
fmt.Print("url scarapping failed")
}
doc.Find("a").Each(func(_ int, s *goquery.Selection) {
url, _ := s.Attr("href")
fmt.Println(url)
})
}
Finally go run, or go build.
$ go build
# or
$ go run scrape.go
Then automatically update go.mod file to read dependent modules and added require part as below. This file includes library version. So you can make same binary using same version libraries.
module your_module_name
require (
github.com/PuerkitoBio/goquery v1.4.1
github.com/andybalholm/cascadia v1.0.0 // indirect
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d // indirect
)
before release
Before release use tidy command to clean up unused require.
$ go mod tidy
How to upgrade go.mod
If you want to upgrade all dependent libraries, you can use below command.
$ go get -u
The go.mod file is updated.
Need I add go.sum into repository?
The go.sum should be checked in along with go.mod. This is checksum of specific version. It helps you to verify library.