Share
Embed
Transcript
75 slides
-
Go modules WARSAW, MAY 22 2019 Oleg Kovalov Allegro Twitter:
oleg_kovalov Github: cristaloleg -
Me - Gopher for ~4 years - Open-source contributor -
Engineer at allegro.pl core team Twitter: @oleg_kovalov Github: @cristaloleg -
- GOPATH - godep - gopkg.in - glide - vendor
dir Pre-modules era (2012-2018) -
- GOPATH - godep - gopkg.in - glide - vendor
dir - dep Pre-modules era (2012-2018) -
- GOPATH - godep - gopkg.in - glide - vendor
dir - dep - ... Pre-modules era (2012-2018) -
- Modules - Semantic versioning - Import compatibility rule -
go.mod Go modules -
- A module is a collection of Go packages that
are versioned as a single unit What is a module? -
- A module is a collection of Go packages that
are versioned as a single unit - (often) a single version-control repository is a single module What is a module? -
What is a module? - A module is a tree/directory
of Go source files with a go.mod file in the root directory -
What is a module? - A module is a tree/directory
of Go source files with a go.mod file in the root directory - Can live outside of a GOPATH -
If an old and a new package have the same
import path, the new package must be backwards-compatible with the old package. Import compatibility rule -
GO111MODULE=auto go build ./… - Inside GOPATH defaults to old
1.10 behavior (ignoring modules) GO111MODULE -
GO111MODULE GO111MODULE=auto go build ./… - Inside GOPATH defaults to
old 1.10 behavior (ignoring modules) - Outside GOPATH while inside a file tree with a go.mod = defaults to modules behavior -
GO111MODULE - auto(default) - on - off $ export GO111MODULE=on
$ go build ./... -
# same (@latest is default for 'go get') $ go
get github.com/gorilla/mux@latest Changes to go get -
# same (@latest is default for 'go get') $ go
get github.com/gorilla/mux@latest # records v1.6.2 $ go get github.com/gorilla/[email protected] Changes to go get -
# same (@latest is default for 'go get') $ go
get github.com/gorilla/mux@latest # records v1.6.2 $ go get github.com/gorilla/[email protected] # records v1.6.2 $ go get github.com/gorilla/mux@e3702bed2 Changes to go get -
# same (@latest is default for 'go get') $ go
get github.com/gorilla/mux@latest # records v1.6.2 $ go get github.com/gorilla/[email protected] # records v1.6.2 $ go get github.com/gorilla/mux@e3702bed2 # records v0.0.0-20180517173623-c85619274f5d $ go get github.com/gorilla/mux@c856192 Changes to go get -
# same (@latest is default for 'go get') $ go
get github.com/gorilla/mux@latest # records v1.6.2 $ go get github.com/gorilla/[email protected] # records v1.6.2 $ go get github.com/gorilla/mux@e3702bed2 # records v0.0.0-20180517173623-c85619274f5d $ go get github.com/gorilla/mux@c856192 # records current meaning of master $ go get github.com/gorilla/mux@master Changes to go get -
$ mkdir /mygo/hello $ cd /mygo/hello $ go mod init
github.com/golang/hello go: creating new go.mod: module github.com/golang/hello $ ls go.mod $ cat go.mod module github.com/golang/hello Let’s start -
$ git clone https://github.com/go-gitea/gitea Cloning into gitea... $ cd gitea
$ go mod init go: creating new go.mod: module github.com/go-gitea/gitea go: copying requirements from Gopkg.lock Convert to modules -
$ git clone https://github.com/go-gitea/gitea Cloning into gitea... $ cd gitea
$ go mod init go: creating new go.mod: module github.com/go-gitea/gitea go: copying requirements from Gopkg.lock $ go mod tidy go: extracting github.com/PuerkitoBio/goquery v1.5.0 go: extracting github.com/blevesearch/bleve v0.7.0 go: finding github.com/go-macaron/session/redis latest go: finding github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57 go: finding golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f go: finding github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b go: finding golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961 go: finding golang.org/x/time v0.0.0-20181108054448-85acf8d2951c go: downloading github.com/facebookgo/grace v0.0.0-20180706040059-75cf19382434 Convert to modules -
module github.com/hashicorp/consul go 1.12 require ( github.com/NYTimes/gziphandler v1.0.1 github.com/DataDog/datadog-go v0.0.0-20160329135253-cc2f4770f4d6
// indirect github.com/go-redis/redis v6.15.2+incompatible ... ) ... How to read go.mod -
How to read go.mod module github.com/hashicorp/consul go 1.12 require (
github.com/NYTimes/gziphandler v1.0.1 github.com/DataDog/datadog-go v0.0.0-20160329135253-cc2f4770f4d6 // indirect github.com/go-redis/redis v6.15.2+incompatible ... ) replace github.com/hashicorp/consul/api => ./api replace github.com/hashicorp/consul/sdk => ./sdk ... -
How to read go.mod module github.com/hashicorp/consul go 1.12 require (
github.com/NYTimes/gziphandler v1.0.1 github.com/DataDog/datadog-go v0.0.0-20160329135253-cc2f4770f4d6 // indirect github.com/go-redis/redis v6.15.2+incompatible ... ) replace github.com/hashicorp/consul/api => ./api replace github.com/hashicorp/consul/sdk => ./sdk exclude github.com/not-a-hacker/supergeil v1270.0.1 -
cloud.google.com/go v0.26.0 h1:e0WKqKTd5BnrG8aKH3J3h+QvEIQtSUcf2n5UZ5ZgLtQ= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/Azure/azure-sdk-for-go v16.0.0+incompatible h1:gr1qKY/Ll72VjFTZmaBwRK1yQHAxCnV25ekOKroc9ws= github.com/Azure/azure-sdk-for-go
v16.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= How to read go.sum -
cloud.google.com/go v0.26.0 h1:e0WKqKTd5BnrG8aKH3J3h+QvEIQtSUcf2n5UZ5ZgLtQ= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/Azure/azure-sdk-for-go v16.0.0+incompatible h1:gr1qKY/Ll72VjFTZmaBwRK1yQHAxCnV25ekOKroc9ws= github.com/Azure/azure-sdk-for-go
v16.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= // you might want to use go mod verify How to read go.sum -
Usage: go mod <command> [arguments] The commands are: download download
modules to local cache edit edit go.mod from tools or scripts fix make go.mod semantically consistent graph print module requirement graph init initialize new module in current directory tidy add missing and remove unused modules vendor make vendored copy of dependencies verify verify dependencies have expected content why explain why packages or modules are needed go mod commands -
# go and get a repo $ go get github.com/go-gitea/gitea
$ cd ~/go/src/github.com/go-gitea/gitea go mod why -
# go and get a repo $ go get github.com/go-gitea/gitea
$ cd ~/go/src/github.com/go-gitea/gitea # set a env var and check why do we use this dependency $ export GO111MODULE=on go mod why -
# go and get a repo $ go get github.com/go-gitea/gitea
$ cd ~/go/src/github.com/go-gitea/gitea # set a env var and check why do we use this dependency $ export GO111MODULE=on $ go mod why github.com/RoaringBitmap/roaring # github.com/RoaringBitmap/roaring code.gitea.io/gitea/modules/indexer github.com/blevesearch/bleve github.com/blevesearch/bleve/index/scorch github.com/RoaringBitmap/roaring go mod why -
FROM golang ENV GO111MODULE=on WORKDIR /app COPY go.mod . COPY
go.sum . Dockerfile -
FROM golang ENV GO111MODULE=on WORKDIR /app COPY go.mod . COPY
go.sum . RUN go mod download COPY . . Dockerfile -
Dockerfile FROM golang ENV GO111MODULE=on WORKDIR /app COPY go.mod .
COPY go.sum . RUN go mod download COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -a -o app.exec ./... EXPOSE 8000 ENTRYPOINT ["./app.exec"] -
Use local changes - Сhange in go.mod manually - go
mod edit -replace ‘foo.com/bar=../bar’ -
Use local changes - Сhange in go.mod manually - go
mod edit -replace ‘foo.com/bar=../bar’ - or use awesome gohack tool by Roger Peppe -
# install $ go get github.com/rogpeppe/gohack $ gohack get example.com/foo/bar
# will clone repo into $HOME/gohack/example.com/foo/bar # will add: replace example.com/foo/bar /home/rog/gohack/example.com/foo/bar gohack by Roger Peppe -
# install $ go get github.com/rogpeppe/gohack $ gohack get example.com/foo/bar
# will clone repo into $HOME/gohack/example.com/foo/bar # will add: replace example.com/foo/bar /home/rog/gohack/example.com/foo/bar $ cat go.mod replace example.com/foo/bar => /home/rog/gohack/example.com/foo/bar gohack by Roger Peppe -
gohack by Roger Peppe # install $ go get github.com/rogpeppe/gohack
$ gohack get example.com/foo/bar # will clone repo into $HOME/gohack/example.com/foo/bar # will add: replace example.com/foo/bar /home/rog/gohack/example.com/foo/bar $ cat go.mod replace example.com/foo/bar => /home/rog/gohack/example.com/foo/bar # to remove specific replace: $ gohack undo example.com/foo/bar # to remove all replaces: $ gohack undo -
- Enabled by default - More robust ecosystem - More
secure Future -
- Enabled by default - More robust ecosystem - More
secure - More tools understand modules Future -
- Enabled by default - More robust ecosystem - More
secure - More tools understand modules - No vendor (perhaps?) Future -
- Enabled by default - More robust ecosystem - More
secure - More tools understand modules - No vendor (perhaps?) - Less “but it works on my machine!” Future