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

Go Packages

Go Packages

Packages & Dependencies

Nathan Youngman

September 22, 2014
Tweet

More Decks by Nathan Youngman

Other Decks in Technology

Transcript

  1. 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()
  2. 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.
  3. Exports Package level.! Initial capital letters are exported.
 func Exported()

    {}
 
 type MyStruct struct {
 unexportedField string
 ExportedField string
 } Takes some getting used to.
  4. 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
 }
  5. Exports package temperature ! type Celsius float64
 type Fahrenheit float64


    
 type Temperature interface {
 Celsius()
 Fahrenheit()
 mustImplement()
 }
  6. 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
  7. 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.
  8. 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.
  9. 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
  10. 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.
  11. Vanity Import Path Import from your domain
 import "yourdomain.com/myproject"! But

    still clone from
 github.com/nathany/myproject! Uses HTML meta tags:
 <meta name="go-import" content="import- prefix vcs repo-root">
  12. 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
  13. One Repository To Rule Them All Copy third-party dependencies into

    main repository.! Travel back through history.! Godep
  14. 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 ./…
  15. 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