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

Implementing a command line client to GitHub in Go

Implementing a command line client to GitHub in Go

My experience of building gh (https://github.com/jingweno/gh) in Go


Jingwen Owen Ou

June 18, 2013

More Decks by Jingwen Owen Ou

Other Decks in Programming


  1. Implementing a command line client to GitHub in Go Owen

    Ou @JingwenOwenOu
  2. Agenda • Automating Git/GitHub workflows with gh • Introduction to

    Go • What I learnt from implementing gh with Go
  3. What’s gh? • a command line tool that makes working

    with GitHub easier • gh pull • gh ci • gh fork • etc.
  4. A broken workflow • git checkout -b new_feature • some

    code changes... • git add . • git commit -m “A comment” • git push origin HEAD • check CI status, oops..context switch • create a pull request, oops...context switch
  5. An optimized workflow • git checkout -b new_feature • some

    code changes... • git add . • git commit -m “A comment” • git push origin HEAD • gh ci # prints build status • gh pull # creates a pull request
  6. Demo

  7. Implementation of gh • Implemented in Go • Fast (40%

    faster than Hub) • A single, statically linked binary with no dependencies (no VM needed!) • Unix, e.g., gh pull -b integration -h new_feature
  8. What’s Go? • Imperative • Object-oriented like • Concurrent •

    Compile to machine code • Created at 2009, v1.1.1
  9. Ken Thompson • Founding father of Unix, see “Coders at

    work” • Bring in regular expression to computing • Created grep in an evening • Designed UTF-8 on a diner placemat
  10. Robert Griesemer • Native code generation for V8 • Java

    HotSpot VM • Strongtalk VM (inspires the Dart VM)
  11. Rob Pike • First window system for Unix at Bell

    Labs • Plan 9 • Co-Authors of “The Unix Programming Environment” & “The Practice of Programming” with Brian Kernighan • Newsqueak, Limbo: implementations of Tony Hoare’s CSP
  12. Why Go? • Why Learn Go? An interview with Rob

    Pike: http://www.youtube.com/watch? v=FTl0tl9BGdc
  13. Hello Go

  14. More about Go • Compilation is very fast, gh has

    1581 LOC, the build time is 0.77s • Static typing & type inference • Low level primitives uint, float64 • Garbage collected • Pointers without pointer arithmetic
  15. Interfaces

  16. The C10K Problem • The problem of optimizing socket server

    software to handle a large number of clients at the same time • C10k = concurrent ten thousand connections • Linux pthreads (8MB), Windows (1MB), Coroutines (4k, e.g., Ruby Fibers)
  17. Goroutines • Coroutines in Go, 4K, light weight threads •

    Segmented stacks (a double linked lists) • No stack-overflow • Automatically scale to multiple-cores • In the future, scale to multiple machines
  18. Goroutines

  19. Channels

  20. Learning from gh • Right tool for the job -

    building a Unix tool • Go’s compiler is freaking fast! • Clarity and simplicity, less is more • Go fmt • Fast startup time, low memory usage • Deploy a static library (making deployment so much simpler!) • Goroutines & channels (higher level APIs in net/ http)
  21. References • gh: https://github.com/jingweno/gh • Code examples: https://gist.github.com/ jingweno/c00e973ade6d66b827fd •

    Go at Google: http://www.infoq.com/ presentations/Go-Google • Concurrency is not Parallelism: http:// vimeo.com/49718712 • Real world Go: https://gist.github.com/ ungerik/3731476
  22. Q&A