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

Go 101 updated

Go 101 updated

Internal introduction to Go at jobandtalent.com. It has some more examples than the one I did at Shopa (also uploaded here).

Avatar for Alexandre González

Alexandre González

November 17, 2015
Tweet

More Decks by Alexandre González

Other Decks in Technology

Transcript

  1. Some history Designed by: Robert Griesemer, Rob Pike & Ken

    Thompson 6 years ago At Google To improve C++ And because they hated it
  2. Why Go? Asynchronous concurrency as part of the language Compiled

    to a single binary Garbage collected Fast Go tools!
  3. Why NOT Go? Libraries not always as mature, but improving!

    No generics... is that a real problem? Dependency management
  4. Your first program p a c k a g e

    m a i n i m p o r t " f m t " f u n c m a i n ( ) { f m t . P r i n t l n ( " H i w o r l d ! " ) } Run
  5. types { u , } i n t { ,

    8 , 1 6 , 3 2 , 6 4 } f l o a t { 3 2 , 6 4 } c o m p l e x { 6 4 , 1 2 8 } b o o l b y t e r u n e "Code point" in Unicode (item represented by a single value). s t r i n g
  6. More types a r r a y s An array

    type definition specifies a length and an element type. s l i c e s A slice is a data structure describing a contiguous section of an array stored separately from the slice variable itself. A slice is not an array. A slice describes a piece of an array. m a p s Key/value storage.
  7. Moarrrrr types s t r u c t s p

    o i n t e r s f u n c t i o n s i n t e r f a c e s t y p e j o b A n d T a l e n t s t r u c t { e m p l o y e e s i n t } f u n c m a i n ( ) { j t : = j o b A n d T a l e n t { e m p l o y e e s : 2 , } f m t . P r i n t l n ( j t . e m p l o y e e s ) } Run
  8. THE Type c h a n "A channel can be

    imagined to be a pipe or a conveyer belt of a defined size and capacity. On one side one can place an item onto this imaginary conveyer belt and on the other side one can take it."
  9. Your second program p a c k a g e

    m a i n f u n c m a i n ( ) { v a r s s t r i n g = " h i " / / N o t s t y l i s h e n o u g h f o r v e t v a r s 2 s t r i n g s 2 = " t h e r e " v a r s 3 = " j o b a n d t a l e n t " s 4 : = " f o l k s " p r i n t l n ( s , s 2 , s 3 , s 4 ) } Run
  10. Named errors v a r N a m e d

    E r r o r = e r r o r s . N e w ( " I s t h i s a n e x c e p t i o n ? " ) f u n c b r e a k S o m e t h i n g ( ) e r r o r { r e t u r n N a m e d E r r o r } f u n c m a i n ( ) { e r r : = b r e a k S o m e t h i n g ( ) s w i t c h e r r { c a s e N a m e d E r r o r : l o g . P r i n t l n ( " F a i l e d a s e x p e c t e d " ) d e f a u l t : l o g . P a n i c ( " I d i d n ' t e x p e c t t h i s ! " ) } i f e r r : = b r e a k S o m e t h i n g ( ) ; e r r = = N a m e d E r r o r { l o g . P r i n t l n ( " T h i s f a i l e d a s e x p e c t e d a s w e l l ! " ) } } Run
  11. JSON t y p e P e r s o

    n s t r u c t { N a m e s t r i n g ` j s o n : " n a m e " ` } f u n c m a i n ( ) { v a r s e n t P e o p l e = [ ] P e r s o n { P e r s o n { " M i h a i " } , P e r s o n { " A l e x " } } p a y l o a d , e r r : = j s o n . M a r s h a l ( s e n t P e o p l e ) i f e r r ! = n i l { l o g . P a n i c ( e r r ) / / D o n ' t d o t h i s a t h o m e } f m t . P r i n t l n ( s t r i n g ( p a y l o a d ) ) v a r r e c e i v e d P e o p l e [ ] P e r s o n i f e r r : = j s o n . U n m a r s h a l ( p a y l o a d , & r e c e i v e d P e o p l e ) ; e r r ! = n i l { l o g . P a n i c ( e r r ) } f o r _ , p : = r a n g e r e c e i v e d P e o p l e { f m t . P r i n t l n ( p . N a m e ) } } Run
  12. Type switch f u n c o u t p

    u t ( w h a t e v e r i n t e r f a c e { } ) { s w i t c h v : = w h a t e v e r . ( t y p e ) { c a s e s t r i n g : f m t . P r i n t l n ( " A S t r i n g : " , v ) c a s e i n t : f m t . P r i n t l n ( " S o m e i n t : " , v ) d e f a u l t : f m t . P r i n t l n ( " I d o n ' t k n o w w h a t t y p e i s t h i s o n e : " , v ) } } f u n c m a i n ( ) { o u t p u t ( " t h i s i s s o m e t e x t " ) o u t p u t ( 1 ) o u t p u t ( 1 . 1 ) } Run
  13. The good one about interfaces! t y p e D

    e v e l o p e r i n t e r f a c e { L i k e s D e v e l o p m e n t ( ) b o o l } t y p e ( G o o d D e v s t r u c t { } M e r c e n a r y D e v s t r u c t { } ) f u n c ( M e r c e n a r y D e v ) L i k e s D e v e l o p m e n t ( ) b o o l { r e t u r n f a l s e } f u n c ( G o o d D e v ) L i k e s D e v e l o p m e n t ( ) b o o l { r e t u r n t r u e } f u n c C o u l d W o r k H e r e ( d D e v e l o p e r ) b o o l { r e t u r n d . L i k e s D e v e l o p m e n t ( ) } f u n c m a i n ( ) { f m t . P r i n t l n ( C o u l d W o r k H e r e ( G o o d D e v { } ) , C o u l d W o r k H e r e ( M e r c e n a r y D e v { } ) ) } Run
  14. Concurrency f u n c m a i n (

    ) { f o r i : = 0 ; i < 1 0 ; i + + { g o f m t . P r i n t l n ( i ) } / / S o m e t h i n g i s g o i n g t o g o w r o n g . . . } Run
  15. ... patterns f u n c d o i t

    ( i i n t , d o n e c h a n b o o l ) { t i m e . S l e e p ( t i m e . D u r a t i o n ( r a n d . I n t n ( 3 ) ) * t i m e . S e c o n d ) f m t . P r i n t l n ( i ) d o n e < - t r u e } f u n c m a i n ( ) { d o n e : = m a k e ( c h a n b o o l , 1 ) f o r i : = 0 ; i < 1 0 ; i + + { g o d o i t ( i , d o n e ) } f o r i : = 0 ; i < 1 0 ; i + + { s e l e c t { c a s e < - d o n e : c o n t i n u e c a s e < - t i m e . A f t e r ( t i m e . S e c o n d * 3 ) : l o g . P r i n t l n ( " T o o . . . m u c h . . . w a i t i n g . . . " ) r e t u r n } } } Run
  16. ... and more patterns f u n c m a

    i n ( ) { v a r w g s y n c . W a i t G r o u p f o r i : = 0 ; i < 1 0 ; i + + { w g . A d d ( 1 ) g o f u n c ( i i n t ) { f m t . P r i n t l n ( i ) w g . D o n e ( ) } ( i ) } w g . W a i t ( ) } Run
  17. OMG

  18. Channels f u n c f i l l (

    q u e u e c h a n < - P e r s o n , w g * s y n c . W a i t G r o u p ) { d e f e r c l o s e ( q u e u e ) d e f e r w g . D o n e ( ) f o r _ , n a m e : = r a n g e [ ] s t r i n g { " M i h a i " , " V i c t o r " , " F r a n c e s c " , " G e r m á n " , " A l e x " } { q u e u e < - P e r s o n { n a m e } t i m e . S l e e p ( t i m e . D u r a t i o n ( r a n d . I n t n ( 1 0 0 0 ) ) * t i m e . M i l l i s e c o n d ) } } f u n c v i s i t ( q u e u e < - c h a n P e r s o n , w g * s y n c . W a i t G r o u p ) { d e f e r w g . D o n e ( ) f o r p : = r a n g e q u e u e { f m t . P r i n t l n ( p . N a m e , " i s v i s i t i n g " ) } } f u n c m a i n ( ) { q u e u e : = m a k e ( c h a n P e r s o n , 2 ) / / . . . Run
  19. Your own server v a r ( c o u

    n t e r i n t p o r t = 9 0 0 0 ) f u n c c o u n t ( 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 ) { c o u n t e r + + } f u n c s t a t s ( 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 ) { w . W r i t e ( [ ] b y t e ( f m t . S p r i n t f ( " V i s i t s : % d " , c o u n t e r ) ) ) } f u n c m a i n ( ) { / / h t t p . H a n d l e F u n c ( " / f a v i c o n . i c o " , f u n c ( _ h t t p . R e s p o n s e W r i t e r , _ * h t t p . R e q u e s t ) { } ) h t t p . H a n d l e F u n c ( " / " , c o u n t ) h t t p . H a n d l e F u n c ( " / s t a t s " , s t a t s ) l o g . P r i n t l n ( " L i s t e n i n g a t p o r t " , p o r t ) l o g . P a n i c ( h t t p . L i s t e n A n d S e r v e ( f m t . S p r i n t f ( " : % d " , p o r t ) , n i l ) ) } Run