Go in a Django World

Go in a Django World

C244b6808a481a0597fc5fc0ca2db42d?s=128

Akshay Shah

October 26, 2013
Tweet

Transcript

  1. Stop Ὂing the Bed Go in a Django World 25

    October 2013 Akshay Shah Resident Gopher, Hearsay Social
  2. This Talk Today, I'm going to convince you that Python

    shouldn't be our only server-side tool One of those tools should be Go Grab some ἷ and strap in.
  3. We Ὂ the Bed Constantly Python & Django are great,

    but they don't do much to prevent the quality problems we're experiencing. In the hour before I wrote this slide, we had 1329 AttributeErrors in production. This is fixable in Python, but we should be free to choose other tools if they're more appropriate - especially for loosely-coupled services.
  4. TANSTAAFL As economists say, there ain't no such thing as

    a free lunch. Python & Django are great at some things, but they're not perfect. Good Bad expressive slow ὐ included clumsy concurrency & parallelism duck-typing runtime errors minimal ceremony poor encapsulation exceptions exceptions In general, we work around Python's weak points with conventions and tests.
  5. Conventions Live Outside the Code From @rbm's bug triage email

    this week: As code quantity, code complexity, and number of developers increase, conventions scale poorly. There's too much tribal knowledge. Said differently, Murphy's Law always wins.
  6. Tests Are Work Testing should be part of any modern

    application. That said, writing tests is work. We need lots of tests to be safe (for the class of errors we're talking about) If we write tests like that, what's the net gain in development velocity? When Python & Django are doing a lot of heavy lifting for us, it's worth writing enough tests to be confident in our code. For tasks that don't leverage much of Django, we should use tools that do more to support us.
  7. Pick Your Poison ☠

  8. Go: Static Typing That Feels Dynamic

  9. Google Juice At a high level, Go is Open source

    Statically typed and compiled to machine code Designed for writing clean server code at scale Feature-wise, it has Great concurrency primitives An unusual take on object orientation ὐ included (but different) It's a language for working programmers, not researchers or academics.
  10. Hello, World p a c k a g e m

    a i n i m p o r t ( " f m t " " n e t / h t t p " ) f u n c m a i n ( ) { h t t p . H a n d l e F u n c ( " / " , f u n c ( w h t t p . R e s p o n s e W r i t e r , r * h t t p . R e q u e s t ) { f m t . F p r i n t f ( w , " H e l l o , 世界" ) } ) h t t p . L i s t e n A n d S e r v e ( " : 1 3 3 7 " , n i l ) } Run
  11. Inference Makes Types Feel Dynamic It also makes refactoring much

    easier. f u n c m a i n ( ) { g r e e t i n g : = " H e l l o , 世界" f m t . P r i n t l n ( g r e e t i n g ) u n i c o d e : = [ ] r u n e ( g r e e t i n g ) f o r p o s , p o i n t : = r a n g e u n i c o d e { f m t . P r i n t f ( " R u n e % d i s a t p o s i t i o n % d . \ n " , p o i n t , p o s ) } } Run
  12. Compiler-Checked Duck Typing t y p e R e a

    d a b l e i n t e r f a c e { R e a d ( p [ ] b y t e ) ( n i n t , e r r e r r o r ) } f u n c h e a d ( r R e a d a b l e ) s t r i n g { b u f : = m a k e ( [ ] b y t e , 5 0 ) n , _ : = r . R e a d ( b u f ) r e t u r n s t r i n g ( b u f [ : n ] ) } f u n c m a i n ( ) { f i l e , _ : = o s . O p e n ( " c o d e / z e n . t x t " ) f m t . P r i n t l n ( h e a d ( f i l e ) ) r e s p o n s e , _ : = h t t p . G e t ( " h t t p : / / h e a r s a y s o c i a l . c o m " ) f m t . P r i n t l n ( h e a d ( r e s p o n s e . B o d y ) ) } Run
  13. Concurrency f u n c m a i n (

    ) { e n g : = g o c h a t . N e w L i s t e n e r ( " E n g i n e e r i n g " ) g o e n g . L i s t e n ( ) i n f r a : = g o c h a t . N e w L i s t e n e r ( " I n f r a s t r u c t u r e " ) g o i n f r a . L i s t e n ( ) f o r { s e l e c t { c a s e m s g : = < - e n g . M e s s a g e s : f m t . P r i n t l n ( " E n g i n e e r i n g : " , m s g . M e s s a g e ) c a s e m s g : = < - i n f r a . M e s s a g e s : f m t . P r i n t l n ( " I n f r a s t r u c t u r e : " , m s g . M e s s a g e ) } } } Run
  14. Error Handling f u n c z e n (

    ) ( t e x t s t r i n g , e r r e r r o r ) { f i l e , e r r : = o s . O p e n ( " c o d e / z e n . t x t " ) i f e r r ! = n i l { r e t u r n } b u f : = m a k e ( [ ] b y t e , 1 0 0 0 ) n , e r r : = f i l e . R e a d ( b u f ) i f e r r ! = n i l { r e t u r n } r e t u r n s t r i n g ( b u f [ : n ] ) , e r r } f u n c m a i n ( ) { z e n , e r r : = z e n ( ) i f e r r ! = n i l { f m t . P r i n t l n ( e r r ) r e t u r n } f m t . P r i n t l n ( z e n ) } Run
  15. Tooling You've already seen most of the core language. Included

    in the standard distribution: Cross-compiling, runtime race detection: go build Source-driven dependency management: go get Unambiguous, tooling-enforced formatting: go fmt Linting: go vet Source-driven documentation: go doc Integrated testing framework: go test Production-ready webserver, routing, and templating. Go takes tooling seriously.
  16. Questions about Go?

  17. Just Add Code If you're interested, the infrastructure is ready.

    golang repo (https://github.com/hearsaycorp/golang) with githooks. Chef recipe (already installed on dev machines) Jenkins job to build, lint, test, and archive packages Deployment from S3
  18. Nobody Likes Ὂ In the Bed

  19. Resources Pro tip: search for "golang," not "go." How to

    Write Go Code (http://golang.org/doc/code.html) The interactive tour (http://tour.golang.org/#1) Effective Go (http://golang.org/doc/effective_go.html) Concurrency Is Not Parallelism (http://www.youtube.com/watch?v=cN_DpYBzKso) There are Django-style mega-frameworks, Flask-style micro-toolkits, BDD and xUnit- style testing, and database-agnostic ORMs available Plugins for emacs, vim, Sublime Text, JetBrains, and Eclipse available
  20. Thank you Akshay Shah Resident Gopher, Hearsay Social akshay@akshayshah.org (mailto:akshay@akshayshah.org)

    http://akshayshah.org (http://akshayshah.org) @akshayshah (http://twitter.com/akshayshah)