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

Go for Rubyists

Avatar for Conrad Irwin Conrad Irwin
February 17, 2015

Go for Rubyists

A tech-talk I gave at Omada Health about why you as a ruby programmer should be excited by Go!

Avatar for Conrad Irwin

Conrad Irwin

February 17, 2015
Tweet

More Decks by Conrad Irwin

Other Decks in Programming

Transcript

  1. Compare and contrast — Rob Pike* Go is efficient, scalable,

    and productive. Some programmers find it fun to work in; others find it unimaginative, even boring. * co‑creator of Go
  2. Compare and contrast — Matz* Ruby is designed to be

    human‑oriented. It reduces the burden of programming. It tries to push jobs back to machines. * creator of Ruby
  3. Differences Go is compiled ahead‑of‑time Goroutines vs. threads Everything is

    an object in Ruby Ruby has object‑oriented inheritance
  4. Hello world! / / m a i n . g

    o / / E v e r y f i l e i s i n a p a c k a g e . I f y o u ' r e w r i t i n g a n a p p u s e " m a i n " , / / o t h e r w i s e i t ' s t h e n a m e o f y o u r l i b r a r y . p a c k a g e m a i n / / Y o u h a v e t o i m p o r t t h e p a c k a g e s y o u n e e d i n e v e r y f i l e . i m p o r t " f m t " / / T h e m a i n f u n c t i o n d e f i n e s w h e r e t h e p r o g r a m s t a r t s . f u n c m a i n ( ) { / / C a l l t h e P r i n t l n m e t h o d i n t h e " f m t " p a c k a g e f m t . P r i n t l n ( " H e l l o W o r l d ! " ) } g o r u n m a i n . g o
  5. Loops, Arrays, etc. f u n c m a i

    n ( ) { / / n o i s y T h i n g s i s a l i s t o f s t r i n g s c o n t a i n i n g " D o g " a n d " C a l t r a i n " n o i s y T h i n g s : = [ ] s t r i n g { " D o g " , " C a l t r a i n " } / / n o i s e s i s a m a p f r o m s t r i n g t o s t r i n g c o n t a i n i n g . . . n o i s e s : = m a p [ s t r i n g ] s t r i n g { " D o g " : " W o o f " , " C a l t r a i n " : " P A A A A A R P " } / / I t e r a t e o v e r t h e r a n g e o f a l l t h e n o i s y T h i n g s . / / T h e _ i s t h e a r r a y i n d e x o f e a c h t h i n g . f o r _ , t h i n g : = r a n g e n o i s y T h i n g s { f m t . P r i n t l n ( t h i n g , " g o e s " , n o i s e s [ t h i n g ] ) } } $ g o r u n m a i n . g o D o g g o e s W o o f C a l t r a i n g o e s P A A A A A R P
  6. Structs / / D e f i n e s

    a n e w t y p e c a l l e d u s e r t h a t h a s t h e f o l l o w i n g f i e l d s . t y p e U s e r s t r u c t { N a m e s t r i n g H o m e p a g e s t r i n g / / p a s s w o r d H a s h s t a r t s w i t h a l o w e r c a s e b e c a u s e i t i s p r i v a t e . p a s s w o r d H a s h [ ] b y t e } / / D e f i n e s a m e t h o d c a l l e d F i r s t N a m e o n t h e u s e r . f u n c ( u s e r * U s e r ) F i r s t N a m e ( ) s t r i n g { / / I n G o s t r i n g s a r e n o t o b j e c t s . / / M e t h o d s a r e o n t h e s t r i n g s p a c k a g e i n s t e a d . r e t u r n s t r i n g s . S p l i t ( u s e r . N a m e , " " ) [ 0 ] } u s e r : = & U s e r { N a m e : " C o n r a d I r w i n " , H o m e p a g e : " h t t p s : / / c i r w . i n " } f m t . P r i n t l n ( u s e r . F i r s t N a m e ( ) )
  7. ?

  8. Duck typing! If it walks like a duck, and it

    quacks like a duck, itʹs pretty much a duck.
  9. In Ruby c l a s s D o g

    d e f s o u n d " W o o f " e n d e n d c l a s s C a l t r a i n d e f s o u n d " P A A A A A A A A A A A A R R P " e n d e n d # I s o b j e c t a D o g o r a C a l t r a i n ? p u t s o b j e c t . s o u n d
  10. In Go t y p e D o g s

    t r u c t { . . . } f u n c ( d o g * D o g ) S o u n d ( ) s t r i n g { r e t u r n " W o o f " } t y p e C a l t r a i n s t r u c t { . . . } f u n c ( c a l t r a i n * C a l t r a i n ) S o u n d ( ) s t r i n g { r e t u r n " P A A A A A A A A A A R P " } / / I s o b j e c t a D o g o r a C a l t r a i n ? f m t . P r i n t l n ( o b j e c t . S o u n d ( ) )
  11. Static duck typing Functions declare which methods they call using

    i n t e r f a c e s Compiler checks that objects passed in implement the interface Itʹs just like ruby, but: Errors show up in vim before I even run my program :)
  12. Interfaces / / A n y o b j e

    c t t h a t h a s a S o u n d ( ) m e t h o d c a n b e u s e d a s a S o u n d e r . t y p e S o u n d e r i n t e r f a c e { S o u n d ( ) s t r i n g } / / F o r e x a m p l e , I c o u l d p a s s e i t h e r a C a l t r a i n o r a D o g : f u n c L i s t e n T o ( s S o u n d e r ) { f m t . P r i n t l n ( " I h e a r d a " , s . S o u n d ( ) ) } L i s t e n T o ( f i d o ) L i s t e n T o ( n o r t h b o u n d 2 3 7 ) $ g o r u n m a i n . g o I h e a r d a W o o f I h e a r d a P A A A A A A A A A A R P
  13. ?

  14. Concurrency In Javascript: (tableflip). In Ruby: Donʹt do that :/.

    In Go: Well‑designed language features :)
  15. Channels f u n c P a y A t

    t e n t i o n ( ) { / / t o d o i s a c h a n n e l t h a t c a n h o l d u p t o 1 0 0 t h i n g s t o d o : = m a k e ( c h a n S o u n d e r , 1 0 0 ) / / F i n d e v e r y t h i n g t o l i s t e n t o i n t h e c u r r e n t e n v i r o n m e n t . f o r s o u n d e r : = r a n g e c u r r e n t E n v i r o n m e n t ( ) { t o d o < - s o u n d e r } / / T h e n l i s t e n t o i t f o r s o u n d e r : = r a n g e t o d o { L i s t e n T o ( s o u n d e r ) } }
  16. Channels Act like a queue (push on one end, pop

    off the other) Writing to a full queue waits until thereʹs space Reading an empty queue waits until someone writes Designed to communicate between goroutines
  17. Goroutines f u n c P a y A t

    t e n t i o n ( ) { / / t o d o i s a c h a n n e l t h a t c a n h o l d u p t o 1 0 0 s o u n d e r s t o d o : = m a k e ( c h a n S o u n d e r , 1 0 0 ) / / L i s t e n t o t h i n g s a s t h e y b e c o m e a v a i l a b l e g o f u n c ( ) { f o r s o u n d e r : = r a n g e t o d o { L i s t e n T o ( s o u n d e r ) } } ( ) / / F i n d e v e r y t h i n g t o l i s t e n t o i n t h e c u r r e n t e n v i r o n m e n t . f o r s o u n d e r : = r a n g e c u r r e n t E n v i r o n m e n t ( ) { t o d o < - s o u n d e r } }
  18. Goroutines Light‑weight threads N goroutines run concurrently using M real

    threads. (M defaults to 1!) Designed to use channels to communicate
  19. Aside: Control f u n c P a y A

    t t e n t i o n ( ) { / / d o n e i s a c h a n n e l t h a t i s u s e d t o s i g n a l w h e n i t ' s d o n e . d o n e : = m a k e ( c h a n b o o l ) t o d o : = m a k e ( c h a n S o u n d e r , 1 0 0 ) g o f u n c ( ) { . . . d o n e < - t r u e } ( ) . . . c l o s e ( t o d o ) / / w a i t u n t i l t h e g o r o u t i n e i s d o n e < - d o n e }
  20. In summary: Go lets you run many things concurrently Go

    lets you hide the concurrency No callbacks required
  21. Why I like go Minimal language Features all work together

    Very powerful abstractions Cross‑compiles everywhere
  22. ?