Slide 1

Slide 1 text

Go modules WARSAW, MAY 22 2019 Oleg Kovalov Allegro Twitter: oleg_kovalov Github: cristaloleg

Slide 2

Slide 2 text

Me - Gopher for ~4 years - Open-source contributor - Engineer at allegro.pl core team Twitter: @oleg_kovalov Github: @cristaloleg

Slide 3

Slide 3 text

Long story short Image credit: ©Istockphoto/JamesBrey time

Slide 4

Slide 4 text

- GOPATH Pre-modules era (2012-2018)

Slide 5

Slide 5 text

- GOPATH - godep - gopkg.in - glide Pre-modules era (2012-2018)

Slide 6

Slide 6 text

- GOPATH - godep - gopkg.in - glide - vendor dir Pre-modules era (2012-2018)

Slide 7

Slide 7 text

- GOPATH - godep - gopkg.in - glide - vendor dir - dep Pre-modules era (2012-2018)

Slide 8

Slide 8 text

- GOPATH - godep - gopkg.in - glide - vendor dir - dep - ... Pre-modules era (2012-2018)

Slide 9

Slide 9 text

Go modules

Slide 10

Slide 10 text

- Modules Go modules

Slide 11

Slide 11 text

- Modules - Semantic versioning Go modules

Slide 12

Slide 12 text

- Modules - Semantic versioning - Import compatibility rule Go modules

Slide 13

Slide 13 text

- Modules - Semantic versioning - Import compatibility rule - go.mod Go modules

Slide 14

Slide 14 text

- A module is a collection of Go packages that are versioned as a single unit What is a module?

Slide 15

Slide 15 text

- 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?

Slide 16

Slide 16 text

What is a module? - A module is a tree/directory of Go source files with a go.mod file in the root directory

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Semantic versioning (http://semver.org)

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

GO111MODULE=auto go build ./… GO111MODULE

Slide 21

Slide 21 text

GO111MODULE=auto go build ./… - Inside GOPATH defaults to old 1.10 behavior (ignoring modules) GO111MODULE

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

- auto(default) - on - off GO111MODULE

Slide 24

Slide 24 text

GO111MODULE - auto(default) - on - off $ export GO111MODULE=on $ go build ./...

Slide 25

Slide 25 text

Changes to go get

Slide 26

Slide 26 text

# same (@latest is default for 'go get') $ go get github.com/gorilla/mux@latest Changes to go get

Slide 27

Slide 27 text

# 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

Slide 28

Slide 28 text

# 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

Slide 29

Slide 29 text

# 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

Slide 30

Slide 30 text

# 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

Slide 31

Slide 31 text

$ 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

Slide 32

Slide 32 text

$ git clone https://github.com/go-gitea/gitea Cloning into gitea... $ cd gitea Convert to modules

Slide 33

Slide 33 text

$ 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

Slide 34

Slide 34 text

$ 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

Slide 35

Slide 35 text

How to read go.mod

Slide 36

Slide 36 text

module github.com/hashicorp/consul ... How to read go.mod

Slide 37

Slide 37 text

module github.com/hashicorp/consul go 1.12 ... How to read go.mod

Slide 38

Slide 38 text

module github.com/hashicorp/consul go 1.12 require ( github.com/NYTimes/gziphandler v1.0.1 ... How to read go.mod

Slide 39

Slide 39 text

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 ... How to read go.mod

Slide 40

Slide 40 text

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 ... How to read go.mod

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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 ...

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

How to read go.sum

Slide 45

Slide 45 text

How to read go.sum

Slide 46

Slide 46 text

cloud.google.com/go v0.26.0 h1:e0WKqKTd5BnrG8aKH3J3h+QvEIQtSUcf2n5UZ5ZgLtQ= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= How to read go.sum

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Usage: go mod [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

Slide 50

Slide 50 text

# go and get a repo $ go get github.com/go-gitea/gitea $ cd ~/go/src/github.com/go-gitea/gitea go mod why

Slide 51

Slide 51 text

# 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

Slide 52

Slide 52 text

# 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

Slide 53

Slide 53 text

FROM golang ENV GO111MODULE=on Dockerfile

Slide 54

Slide 54 text

FROM golang ENV GO111MODULE=on WORKDIR /app COPY go.mod . COPY go.sum . Dockerfile

Slide 55

Slide 55 text

FROM golang ENV GO111MODULE=on WORKDIR /app COPY go.mod . COPY go.sum . RUN go mod download COPY . . Dockerfile

Slide 56

Slide 56 text

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"]

Slide 57

Slide 57 text

Use local changes

Slide 58

Slide 58 text

Use local changes - Сhange in go.mod manually

Slide 59

Slide 59 text

Use local changes - Сhange in go.mod manually - go mod edit -replace ‘foo.com/bar=../bar’

Slide 60

Slide 60 text

Use local changes - Сhange in go.mod manually - go mod edit -replace ‘foo.com/bar=../bar’ - or use awesome gohack tool by Roger Peppe

Slide 61

Slide 61 text

# install $ go get github.com/rogpeppe/gohack gohack by Roger Peppe

Slide 62

Slide 62 text

# 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

Slide 63

Slide 63 text

# 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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

Go Proxy

Slide 66

Slide 66 text

Go Proxy https://proxy.golang.org https://github.com/gomods/athens - export GOPROXY=my.proxy.com - go get github.com/foo/bar

Slide 67

Slide 67 text

Future

Slide 68

Slide 68 text

- Enabled by default Future

Slide 69

Slide 69 text

- Enabled by default - More robust ecosystem Future

Slide 70

Slide 70 text

- Enabled by default - More robust ecosystem - More secure Future

Slide 71

Slide 71 text

- Enabled by default - More robust ecosystem - More secure - More tools understand modules Future

Slide 72

Slide 72 text

- Enabled by default - More robust ecosystem - More secure - More tools understand modules - No vendor (perhaps?) Future

Slide 73

Slide 73 text

- Enabled by default - More robust ecosystem - More secure - More tools understand modules - No vendor (perhaps?) - Less “but it works on my machine!” Future

Slide 74

Slide 74 text

Why package management is hard?

Slide 75

Slide 75 text

That’s all folks Thank you Questions? Twitter: @oleg_kovalov Github: @cristaloleg