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

Why Go

Why Go

Introduction to the Go language

Victor Castell

February 04, 2014
Tweet

More Decks by Victor Castell

Other Decks in Programming

Transcript

  1. MINDS BEHIND ROB PIKE: UNIX, PLAN 9 KEN THOMPSON: UNIX,

    PLAN 9, B AND C RUSS COX: PLAN 9, RE2 ROBERT GRIESEMER: V8, JAVA VM BRAD FITZPATRICK, ANDREW GERRAND + LOTS OF CONTRIBUTORS
  2. PRECEDENTS NEED FOR HIGH-LEVEL PROGRAMMING IN UNIX C++ BECAME POPULAR

    IN THE INDUSTRY AND RESEARCH JAVA - EASY C++
  3. CONSECUENCES C++ AND JAVA ARE HARD TO USE VERBOSE AND

    DIFFICULT TO READ, IDE REQUIRED WE NEED "PATTERNS" TO ADDRESS IT'S DIFFICULTY WIDELY ADOPTED TODAY PERFORMANT
  4. THE GOOD EFFICCIENT, PERFORMAT AND SAFE VERY POWERFUL IN EXPERT

    HANDS REALLY BIG SYSTEMS ARE BUILT ON THEM ADAPTS WELL TO CORPORATE ENVIRONMENTS: LOTS OF CODERS, BIG SYSTEMS
  5. THE BAD NOT DEVELOPER FRIENDLY SLOW COMPILERS AND HUGE BINARIES

    PROGRAMMERS AVOID OLD AND NOT SUITTED TO MULTICORE JVM HAVE LIMITS IN MEMORY MANAGEMENT
  6. DYNAMIC DON'T HAVE THE DOWNSIDES LESS KEYSTROKES BECAUSE OF DINAMYC

    TYPING NO COMPILE TIME BUT THEY ALSO HAVE THE "BAD": Slow Not type-safe (static errors occur at runtime) Doesn't scale well They're also not very modern (+10 years)
  7. WE NEED THE GOOD, WITHOUT THE BAD, AND MULTICORE: STATICALLY

    TYPED FAST EXECUTION, FAST COMPILERS LIGHT AND EASY TO WRITE FAST TO WORK IN SCALES WELL DOESN'T REQUIRE TOOLS, BUT SUPPORTS THEM WELL GOOD AT NETWORKING AND MULTIPROCESSING
  8. ENTER GO Fun, efficient and open source Statically typed and

    compiled, but it feels lightweight and dynamic Designed for modern computing (multicore) Aimed at software such as webservers, but turned out to be a great general-purpose language
  9. HELLO WORLD 2.0 / / S e r v i

    n g h t t p : / / l o c a l h o s t : 8 0 8 0 / w o r l d : 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 " " l o g " ) 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 , % q " , r . U R L . P a t h [ 1 : ] ) } ) l o g . F a t a l ( h t t p . L i s t e n A n d S e r v e ( " : 8 0 8 0 " , n i l ) ) }
  10. HIGHLIGHTS Multiple return values f u n c f o

    o ( ) i n t , s t r i n g { r e t u r n 1 , " O K " } Named return parameters f u n c f o o ( ) ( b a r i n t ) { b a r = 2 + 2 r e t u r n }
  11. who, will have the value VARIADIC INPUT PARAMETERS f u

    n c G r e e t i n g ( p r e f i x s t r i n g , w h o . . . s t r i n g ) G r e e t i n g ( " h e l l o : " , " J o e " , " A n n a " , " E i l e e n " ) [ ] s t r i n g { " J o e " , " A n n a " , " E i l e e n " }
  12. CLOSURES ARE JUST LOCAL FUNCTIONS f u n c f

    o o ( f f u n c ( ) ) { x : = f ( ) f u n c ( ) { p r i n t l n ( x ) } ( ) }
  13. FEATURES TYPE INFERENCE f o o : = " T

    h i s i s a s t r i n g t y p e " INTERFACES t y p e F o o i n t e r f a c e { p r i v a t e M e t h o d ( ) i n t P u b l i c M e t h o d ( ) i n t }
  14. FEATURES INTERFACES ARE SATISFIED IMPLICITLY, EVERY TYPE THAT IMPLEMENTS IT'S

    METHODS IS SATISFYING THE INTERFACE THIS IS VERY POWERFUL!
  15. CONCURRENCY BASICS Start a goroutine g o f ( )

    Channel sends c h < - v a l u e Channel receive v a l u e = < - c h Starting a goroutine f u n c ( s * S e r v i c e ) S t a r t ( ) c h a n r e q u e s t { c h : = m a k e ( c h a n r e q u e s t ) g o s . s e r v e ( c h ) / / s . s e r v e r u n s c o n c u r r e n t l y r e t u r n c h / / r e t u r n s i m m e d i a t e l y }
  16. DEPENDENCY MANAGEMENT Dependencies are expressed in source code i m

    p o r t " g i t h u b . c o m / b m i z e r a n y / p a t " Two purposes, import path and library repository Just push your code to a public repository The "go get" command download and install all rependencies Easy to define, easy to use
  17. OTHER FEATURES Emphasis on simplicity Garbage collected Memory layout control

    (pointers but no pointer arithmetic) Consistent standard library Integrated testing Ultra fast compiler (can be used as scripting language) Target i386, amd64, arm (Mobile development) BUT.. AS ANY RESPECTED LANGUAGE ALSO HAS DRAMAS!
  18. STATUS Go 1 released 28/03/2012 Easy installation: Binaries for OSX,

    Linux, Windows Stable, mature and suitable for real world problems Used in production at big companies Very active community and growing fast
  19. ERLANG VS SCALA VS GO Erlang and Scala implements the

    Actor model, Go implements goroutines Scala has thread based Actors that are heavyweight goroutines are lightweight, threads are heavy All three models uses channels for messages In Scala passing around primitive data like integers is expensive because of the JVM legacy
  20. THE INEVITABLE COMPARISON node.js/JS/V8 doesn't have concurrency features but comparable

    for server programming Awesome standard lib but not enought Go stdlib, http, templates, websockets, tar/zip, json, xml, sql, smtp, etc... node code is by definition non-blocking and asynchronous Go let's you write synchronous looking code which behaves like if it were asynchronous, without requiring you to handle the callback spaghetti Many libs in Go stdlib are non-blocking
  21. THE INEVITABLE COMPARISON Fibonacci sequence made by the infamous post

    http://teddziuba.com/2011/10/node-js-is-cancer.html node.js v a r h t t p = r e q u i r e ( ' h t t p ' ) ; f u n c t i o n f i b o n a c c i ( n ) { i f ( n < 2 ) r e t u r n 1 ; e l s e r e t u r n f i b o n a c c i ( n - 2 ) + f i b o n a c c i ( n - 1 ) ; } h t t p . c r e a t e S e r v e r ( f u n c t i o n ( r e q , r e s ) { r e s . w r i t e H e a d ( 2 0 0 , { ' C o n t e n t - T y p e ' : ' t e x t / p l a i n ' } ) ; r e s . e n d ( f i b o n a c c i ( 4 0 ) + ' ' ) ; } ) . l i s t e n ( 1 3 3 7 ) ;
  22. THE INEVITABLE COMPARISON Go 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 " " l o g " ) f u n c f i b o n a c c i ( n i n t ) ( r e s u l t i n t ) { i f n < 2 { r e s u l t = 1 } e l s e { r e s u l t = f i b o n a c c i ( n - 2 ) + f i b o n a c c i ( n - 1 ) } r e t u r n } 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 e q * h t t p . R e q u e s t ) { w . W r i t e ( [ ] b y t e ( f m t . S p r i n t f ( " % d " , f i b o n a c c i ( 4 0 ) ) ) ) } ) l o g . F a t a l ( h t t p . L i s t e n A n d S e r v e ( " : 8 0 8 0 " , n i l ) ) }
  23. SHOW ME THE NUMBERS! node.js 0.10.16 t i m e

    c u r l h t t p : / / l o c a l h o s t : 1 3 3 7 1 6 5 5 8 0 1 4 1 r e a l 0 m 2 . 0 9 7 s u s e r 0 m 0 . 0 0 7 s s y s 0 m 0 . 0 0 5 s Go 1.2 t i m e c u r l h t t p : / / l o c a l h o s t : 8 0 8 0 1 6 5 5 8 0 1 4 1 r e a l 0 m 1 . 3 2 3 s u s e r 0 m 0 . 0 0 7 s s y s 0 m 0 . 0 0 5 s