Slide 1

Slide 1 text

Packages & Dependencies @nathany Go gopher designed by Renée French.

Slide 2

Slide 2 text

Packages Package names correspond to a folder of the same name.
 package weather Import strings contain the package path and name.
 import “my/weather" // $GOPATH/src/my/weather! Qualify with the package name.
 weather.Precipitation()! A different name can be chosen.
 import rain "my/weather"
 rain.Precipitation()

Slide 3

Slide 3 text

Initialization Global variables are initialized at runtime:
 var port = os.Getenv("PORT")! You can declare special init functions in each file.
 func init() {
 }! Initialization is well defined.! Dependencies initialize first.

Slide 4

Slide 4 text

Exports Package level.! Initial capital letters are exported.
 func Exported() {}
 
 type MyStruct struct {
 unexportedField string
 ExportedField string
 } Takes some getting used to.

Slide 5

Slide 5 text

Exports Never wonder if a symbol is exported or not.! Even at the call site.! In a diff during code review.! Reflection package requires symbols to be exported. type info struct {
 ImportPath string
 Rev string
 }

Slide 6

Slide 6 text

Exports package temperature ! type Celsius float64
 type Fahrenheit float64
 
 type Temperature interface {
 Celsius()
 Fahrenheit()
 mustImplement()
 }

Slide 7

Slide 7 text

Internal Packages What if parts of your API aren’t yet stable?! But you still want to organize code into packages.! fsnotify/internal/kqueue
 fsnotify
 fsnotify/cmd Go 1.4

Slide 8

Slide 8 text

Hoisting fmt reflect strconv Go compilers are fast.! No circular dependencies.

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Package Names apt-get install docker System tray for KDE3/GNOME2 docklet applications.! apt-get install node Amateur Packet Radio Node program! In contrast, go get doesn’t rely on a global namespace.

Slide 11

Slide 11 text

Packages From the Network The go tool introduces a convention:! go get github.com/nathany/myproject clones to:
 src/github.com/nathany/myproject! import "github.com/nathany/myproject" Import paths are just strings.! The go compilers look for files on disk.

Slide 12

Slide 12 text

Go Get, Fork Later go get -u github.com/nathany/myproject git remote add fork [email protected]:mycompany/myproject.git git push fork my-new-feature Contributing to Open Source Git Repositories in Go

Slide 13

Slide 13 text

Glorified Clone? Nope. Gets dependencies too. ! Not limited to GitHub.

Slide 14

Slide 14 text

Gopkg.in Major version numbers.
 import "gopkg.in/go-fsnotify/fsnotify.v0" A special convention.
 import "gopkg.in/fsnotify.v1"! Based on tags and branches such as v1.0.3.! Well suited for libraries.

Slide 15

Slide 15 text

Vanity Import Path Import from your domain
 import "yourdomain.com/myproject"! But still clone from
 github.com/nathany/myproject! Uses HTML meta tags:


Slide 16

Slide 16 text

Canonical Import Paths You must import this package from here.
 package pdf // import "rsc.io/pdf"! v0 branch
 package fsnotify // import “gopkg.in/fsnotify.v0”
 
 v1 branch
 package fsnotify // import “gopkg.in/fsnotify.v1" Go 1.4

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

One Repository To Rule Them All Copy third-party dependencies into main repository.! Travel back through history.! Godep

Slide 19

Slide 19 text

Godep godep get github.com/coreos/nova-agent-watcher! godep save! The dependencies are stored right in the repo:
 Godeps/_workspace/src/ godep go test ./…

Slide 20

Slide 20 text

Godep Two modes:! Modifies $GOPATH! Rewrite import paths (-r)

Slide 21

Slide 21 text

Resources Contributing to Open Source Git Repositories in Go
 https://blog.splice.com/contributing-open-source-git- repositories-go/! gopkg.in
 http://labix.org/gopkg.in! Godep
 https://github.com/tools/godep! Go 1.4 Custom Import Path Checking
 http://golang.org/s/go14customimport! Go 1.4 “Internal” Packages
 http://golang.org/s/go14internal