Slide 1

Slide 1 text

Stop Ὂing the Bed Go in a Django World 25 October 2013 Akshay Shah Resident Gopher, Hearsay Social

Slide 2

Slide 2 text

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.

Slide 3

Slide 3 text

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.

Slide 4

Slide 4 text

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.

Slide 5

Slide 5 text

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.

Slide 6

Slide 6 text

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.

Slide 7

Slide 7 text

Pick Your Poison ☠

Slide 8

Slide 8 text

Go: Static Typing That Feels Dynamic

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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.

Slide 16

Slide 16 text

Questions about Go?

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Nobody Likes Ὂ In the Bed

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Thank you Akshay Shah Resident Gopher, Hearsay Social [email protected] (mailto:[email protected]) http://akshayshah.org (http://akshayshah.org) @akshayshah (http://twitter.com/akshayshah)