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

Go At Work

Ba4b0b302231bffb0f685bbef25db0d6?s=47 Bryan Liles
December 18, 2014

Go At Work

You know how to program Go. How do you program Go at work?

Ba4b0b302231bffb0f685bbef25db0d6?s=128

Bryan Liles

December 18, 2014
Tweet

More Decks by Bryan Liles

Other Decks in Programming

Transcript

  1. digitalocean.com Go At Work

  2. digitalocean.com Go At DigitalOcean

  3. digitalocean.com Bryan Liles @bryanl @digitalocean

  4. digitalocean.com Wow GO!

  5. digitalocean.com

  6. digitalocean.com Dependencies :|

  7. digitalocean.com package main import "github.com/go-martini/martini" func main() { m :=

    martini.Classic() m.Get("/", func() string { return "Hello world!" }) m.Run() }
  8. digitalocean.com package main import ( "fmt" "net/http" "github.com/codegangsta/negroni" ) func

    main() { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { fmt.Fprintf(w, "Hello world!") }) n := negroni.Classic() n.UseHandler(mux) n.Run(":3000") }
  9. digitalocean.com package main import ( "fmt" "net/http" ) func handler(w

    http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello world!") } func main() { http.HandleFunc("/", handler) http.ListenAndServe(":3000", nil) }
  10. digitalocean.com package main import ( "fmt" "net/http" "github.com/gorilla/mux" ) func

    main() { r := mux.NewRouter() r.HandleFunc("/", homeHandler) http.Handle("/", r) http.ListenAndServe(":3000", nil) } func homeHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello world!") }
  11. digitalocean.com “You wouldn’t go around picking stuff off the street

    and eating it. Why do so with your dependencies?”
  12. digitalocean.com Fancy Stuff Standard Lib

  13. digitalocean.com Standard Lib Fancy Stuff

  14. digitalocean.com Standard Lib Fancy Stuff

  15. digitalocean.com Fancy Stuff Standard Lib

  16. digitalocean.com Managing the GOPATH

  17. digitalocean.com There’s more than one way to do it.

  18. digitalocean.com single GOPATH

  19. digitalocean.com GOPATH for each project

  20. digitalocean.com Dependency manager

  21. digitalocean.com Go @digitalocean

  22. digitalocean.com single GOPATH

  23. digitalocean.com a DOGE

  24. digitalocean.com vendor repos import "bits.do.co/vendor/cli"

  25. digitalocean.com Beyond gofmt

  26. digitalocean.com Sometimes the default sorting of your imports just doesn’t

    satisfy…
  27. digitalocean.com import ( "database/sql" "doge/log" "doge/notify" "fmt" "os" "services/migration" "time"

    _ "github.com/go-sql-driver/mysql" "github.com/ianschenck/envflag" )
  28. digitalocean.com import ( "database/sql" "fmt" "os" "time" "doge/log" "doge/notify" "services/migration"

    _ "github.com/go-sql-driver/mysql" "github.com/ianschenck/envflag" )
  29. digitalocean.com http://golang.org/pkg/go/parser/ (and friends)

  30. digitalocean.com Test the GO

  31. digitalocean.com You are writing tests, right?

  32. digitalocean.com func TestSquare(t *testing.T) { expected := 25 got :=

    square(5) if got != expected { t.Errorf("expected %d, got %d", expected, got) } }
  33. digitalocean.com undefined: square

  34. digitalocean.com func square(x int) int { return 0 }

  35. digitalocean.com --- FAIL: TestSquare (0.00 seconds) square_test.go:14: expected 25, got

    0 FAIL
  36. digitalocean.com func square(x int) int { return 25 }

  37. digitalocean.com $ go test . ok _/Users/bryan/gothamgo2014 0.005s

  38. digitalocean.com func TestSquare0(t *testing.T) { expected := 0 got :=

    square(0) if got != expected { t.Errorf("expected %d, got %d", expected, got) } }
  39. digitalocean.com --- FAIL: TestSquare0 (0.00 seconds) square_test.go:23: expected 0, got

    25 FAIL
  40. digitalocean.com func square(x int) int { return x * x

    }
  41. digitalocean.com go test . ok _/Users/bryan/gothamgo2014 0.006s

  42. digitalocean.com func TestSquare(t *testing.T) { cases := []struct { arg

    int expected int }{ {5, 25}, {0, 0}, {-1, 1}, } for i, c := range cases { got := square(c.arg) if got != c.expected { t.Errorf("case %d: expected %d, got %d", i, c.expected, got) } } }
  43. digitalocean.com looper https://github.com/nathany/looper

  44. digitalocean.com

  45. digitalocean.com GoConvey http://goconvey.co/

  46. digitalocean.com

  47. digitalocean.com • 10 things you (probably) don't know about Go

    • Testing Techniques
  48. digitalocean.com Code Reviews

  49. digitalocean.com https://code.google.com/p/go-wiki/wiki/CodeReviewComments Start here

  50. digitalocean.com WHY > HOW

  51. digitalocean.com Continuous Integration

  52. digitalocean.com Drone CI

  53. digitalocean.com We ❤️ Artifacts

  54. digitalocean.com Deploying GO

  55. digitalocean.com Little Deploys = ansible

  56. digitalocean.com Big Deploys = Chef

  57. digitalocean.com

  58. digitalocean.com The End.