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

Go Packages

Go Packages

Packages & Dependencies

7d0978b275a362d11283396a929aee65?s=128

Nathan Youngman

September 22, 2014
Tweet

More Decks by Nathan Youngman

Other Decks in Technology

Transcript

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

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

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


    
 type Temperature interface {
 Celsius()
 Fahrenheit()
 mustImplement()
 }
  7. 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
  8. Hoisting fmt reflect strconv Go compilers are fast.! No circular

    dependencies.
  9. None
  10. 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.
  11. 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.
  12. Go Get, Fork Later go get -u github.com/nathany/myproject git remote

    add fork git@github.com:mycompany/myproject.git git push fork my-new-feature Contributing to Open Source Git Repositories in Go
  13. Glorified Clone? Nope. Gets dependencies too. ! Not limited to

    GitHub.
  14. 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.
  15. 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">
  16. 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
  17. None
  18. One Repository To Rule Them All Copy third-party dependencies into

    main repository.! Travel back through history.! Godep
  19. 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 ./…
  20. Godep Two modes:! Modifies $GOPATH! Rewrite import paths (-r)

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