1 of 29

Modules

18 July 2018

Reston, VA

Chris Hines

Comcast

@chris_csguy

2 of 29

Modules

Executive Summary

The Road Here

Structure

User Experience

Inner Workings

The Road Ahead

3 of 29

Executive Summary

Modules

  • Versioned Packages
  • Built Into the Go Tool
  • Repeatable Builds
  • No GOPATH

{

4 of 29

The Road Here

Modules

5 of 29

Milestones

The Road Here

  • GOPATH 2012 Go 1.0
  • goven 2012
  • godep 2013
  • gopkg.in 2014
  • glide 2014
  • gb 2015 (April)
  • govendor 2015 (April)
  • vendor dir 2015 (June) Go 1.5 -> 1.6
  • dep 2017
  • go mod 2018 Go 1.11 -> 1.12

6 of 29

GOPATH

The Road Here

src/

github.com/golang/example/

.git/ # Git repository metadata

hello/

hello.go # command source

outyet/

main.go # command source

main_test.go # test source

stringutil/

reverse.go # package source

reverse_test.go # test source

golang.org/x/image/

.git/ # Git repository metadata

bmp/

reader.go # package source

writer.go # package source

7 of 29

GOPATH

The Road Here

Configuration by convention enabled

  • go get
  • go build
  • go test

8 of 29

Nope

Repeatable Builds?

9 of 29

Experimentation

The Road Here

  • GOPATH 2012 Go 1.0
  • goven 2012
  • godep 2013
  • gopkg.in 2014
  • glide 2014
  • gb 2015 (April)
  • govendor 2015 (April)
  • vendor dir 2015 (June) Go 1.5 -> 1.6
  • dep 2017
  • go mod 2018 Go 1.11 -> 1.12

10 of 29

Codification

The Road Here

  • GOPATH 2012 Go 1.0
  • goven 2012
  • godep 2013
  • gopkg.in 2014
  • glide 2014
  • gb 2015 (April)
  • govendor 2015 (April)
  • vendor dir 2015 (June) Go 1.5 -> 1.6
  • dep 2017
  • go mod 2018 Go 1.11 -> 1.12

11 of 29

Unification

The Road Here

  • GOPATH 2012 Go 1.0
  • goven 2012
  • godep 2013
  • gopkg.in 2014
  • glide 2014
  • gb 2015 (April)
  • govendor 2015 (April)
  • vendor dir 2015 (June) Go 1.5 -> 1.6
  • dep 2017
  • go mod 2018 Go 1.11 -> 1.12

12 of 29

Redesign

The Road Here

  • GOPATH 2012 Go 1.0
  • goven 2012
  • godep 2013
  • gopkg.in 2014
  • glide 2014
  • gb 2015 (April)
  • govendor 2015 (April)
  • vendor dir 2015 (June) Go 1.5 -> 1.6
  • dep 2017
  • go mod 2018 Go 1.11 -> 1.12

13 of 29

Structure

Modules

14 of 29

A module is

Structure

  • A collection of related Go packages
  • The unit of code interchange and versioning

15 of 29

Filesystem Structure

Structure

<any-path>/

go.mod # module file

main.go # command source

<any-path>/

.git/ # optional

go.mod # identifies the module root

hello.go # package source

hello_test.go # test source

stringutil/

reverse.go # package source

reverse_test.go # test source

16 of 29

go.mod

Structure

module example.com/hello

Module Path

The import path prefix of the module.

17 of 29

go.mod

Structure

module example.com/hello

require (

golang.org/x/text v0.3.0

gopkg.in/yaml.v2 v2.1.0

)

Dependencies

List of modules we depend on and the minimum version we can use.

18 of 29

go.mod

Structure

module example.com/hello

require (

golang.org/x/text v0.3.0

gopkg.in/yaml.v2 v2.1.0

)

exclude github.com/go-stack/stack v1.6.0

Exclusions

List of module versions we reject for some reason.

19 of 29

go.mod

Structure

module example.com/hello

require (

golang.org/x/text v0.3.0

gopkg.in/yaml.v2 v2.1.0

)

exclude github.com/go-stack/stack v1.6.0

replace (

github.com/go-stack/stack v1.4.0 => ../stack/

golang.org/x/text => github.com/pkg/errors v0.8.0

)

Replacements

List of module [versions] we replace with something else.

20 of 29

User Experience

Modules

21 of 29

Go 1.11 Module Support

User Experience

  • Inside GOPATH — Status Quo
  • Outside GOPATH — Modules reign
  • GO111MODULE environment variable
    • auto (default)
    • on
    • off

22 of 29

Commands

User Experience

go mod -init [-module=module-path]

go build

go test

go list -m [-u] all

go mod -sync

go help modules

go help mod

23 of 29

go get (module remix)

User Experience

go get github.com/gorilla/mux@latest

same (@latest is default for 'go get')

go get github.com/gorilla/mux@v1.6.2

records v1.6.2

go get github.com/gorilla/mux@e3702bed2

records v1.6.2

go get github.com/gorilla/mux@c856192

records v0.0.0-20180517173623-c85619274f5d

go get github.com/gorilla/mux@master

records current meaning of master

24 of 29

Live demo

User Experience

25 of 29

Inner Workings

Modules

26 of 29

Live Demo

Inner Workings

27 of 29

The Road Ahead

Modules

28 of 29

Release Plans

The Road Ahead

  • Module support merged July 12
  • Opt-in feature in Go 1.11 (end of Aug?)
  • Default in Go 1.12

29 of 29

Ultimate Goals

The Road Ahead

  • Add versions to the Go vocabulary of
    • Developers
    • Tools
  • Break free of GOPATH
  • Minimize need for vendor dirs