Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go modules

Go modules

Oleg Kovalov

May 22, 2019
Tweet

More Decks by Oleg Kovalov

Other Decks in Programming

Transcript

  1. Me - Gopher for ~4 years - Open-source contributor -

    Engineer at allegro.pl core team Twitter: @oleg_kovalov Github: @cristaloleg
  2. - GOPATH - godep - gopkg.in - glide - vendor

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

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

    dir - dep - ... Pre-modules era (2012-2018)
  5. - A module is a collection of Go packages that

    are versioned as a single unit What is a module?
  6. - 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?
  7. What is a module? - A module is a tree/directory

    of Go source files with a go.mod file in the root directory
  8. 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
  9. 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
  10. GO111MODULE=auto go build ./… - Inside GOPATH defaults to old

    1.10 behavior (ignoring modules) GO111MODULE
  11. 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
  12. # same (@latest is default for 'go get') $ go

    get github.com/gorilla/mux@latest Changes to go get
  13. # 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
  14. # 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
  15. # 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
  16. # 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
  17. $ 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
  18. $ 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
  19. $ 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
  20. 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 ...
  21. 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
  22. 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
  23. # go and get a repo $ go get github.com/go-gitea/gitea

    $ cd ~/go/src/github.com/go-gitea/gitea go mod why
  24. # 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
  25. # 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
  26. FROM golang ENV GO111MODULE=on WORKDIR /app COPY go.mod . COPY

    go.sum . RUN go mod download COPY . . Dockerfile
  27. 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"]
  28. Use local changes - Сhange in go.mod manually - go

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

    mod edit -replace ‘foo.com/bar=../bar’ - or use awesome gohack tool by Roger Peppe
  30. # 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
  31. # 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
  32. 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
  33. - Enabled by default - More robust ecosystem - More

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

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

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