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

Gopher's talk

Gopher's talk

Introduction to Golang. Lunchbox meeting@ibuildings

Martijn De Letter

November 25, 2015
Tweet

More Decks by Martijn De Letter

Other Decks in Programming

Transcript

  1. Go-lang Go is an open source programming (multi-purpose) language that

    makes it easy to build simple, reliable, and efficient software. Modern, small language Concurrency build-in Compiles to machine code Fast, super fast Staticlly typed, but feels like a dynamically typed language Designed by programmers for programmers!
  2. Gopher The Go gopher is an iconic mascot and one

    of the most distinctive features of the Go project
  3. Interfaces Almost everything uses a interface Small interfaces t y

    p e S t r i n g e r i n t e r f a c e { S t r i n g ( ) s t r i n g } t y p e e r r o r i n t e r f a c e { E r r o r ( ) s t r i n g } t y p e S t o r e i n t e r f a c e { S e t ( u r l s t r i n g , c o u n t i n t ) e r r o r G e t ( u r l s t r i n g ) ( c o u n t i n t , e r r e r r o r ) } "No implements"
  4. Defer f u n c m a i n (

    ) { f : = c r e a t e F i l e ( " / t m p / d e f e r . t x t " ) d e f e r c l o s e F i l e ( f ) w r i t e F i l e ( f ) }
  5. Structs Objects / / r e p r e s

    e n t s t h e j o b t o b e r u n t y p e J o b s t r u c t { U r l s t r i n g S t o r e S t o r e } / / l e t ' s c r e a t e a j o b w o r k : = J o b { U r l : u r l , S t o r e : s } Exportable: Share vs share
  6. Struct - json json handling build-in t y p e

    G r a p h s t r u c t { I d s t r i n g ` j s o n : " i d " ` S h a r e s i n t ` j s o n : " s h a r e s " ` C o m m e n t s i n t ` j s o n : " c o m m e n t s " ` } v a r g G r a p h e r r = j s o n . U n m a r s h a l ( b o d y , & g )
  7. Go routines p a c k a g e m

    a i n i m p o r t ( " f m t " " m a t h / r a n d " " t i m e " ) f u n c m a i n ( ) { g o b o r i n g ( " b o r i n g ! " ) f m t . P r i n t l n ( " I ' m l i s t e n i n g . " ) t i m e . S l e e p ( 2 * t i m e . S e c o n d ) f m t . P r i n t l n ( " Y o u ' r e b o r i n g ; I ' m l e a v i n g . " ) } Run
  8. Go routines - performance p a c k a g

    e m a i n i m p o r t " f m t " f u n c f ( l e f t , r i g h t c h a n i n t ) { l e f t < - 1 + < - r i g h t } f u n c m a i n ( ) { c o n s t n = 1 0 0 0 0 l e f t m o s t : = m a k e ( c h a n i n t ) r i g h t : = l e f t m o s t l e f t : = l e f t m o s t f o r i : = 0 ; i < n ; i + + { r i g h t = m a k e ( c h a n i n t ) g o f ( l e f t , r i g h t ) l e f t = r i g h t } g o f u n c ( c c h a n i n t ) { c < - 1 } ( r i g h t ) f m t . P r i n t l n ( < - l e f t m o s t ) } Run
  9. Channels communication between go routines send and receive use tickers

    and more... c h < - v / / S e n d v t o c h a n n e l c h . v : = < - c h / / R e c e i v e f r o m c h , a n d a s s i g n t o v / / C r e a t e c h a n n e l c h : = m a k e ( c h a n i n t )
  10. Channels - next level Search engine with replica's and timeouts

    c : = m a k e ( c h a n R e s u l t ) g o f u n c ( ) { c < - F i r s t ( q u e r y , W e b 1 , W e b 2 ) } ( ) g o f u n c ( ) { c < - F i r s t ( q u e r y , I m a g e 1 , I m a g e 2 ) } ( ) g o f u n c ( ) { c < - F i r s t ( q u e r y , V i d e o 1 , V i d e o 2 ) } ( ) t i m e o u t : = t i m e . A f t e r ( 8 0 * t i m e . M i l l i s e c o n d ) f o r i : = 0 ; i < 3 ; i + + { s e l e c t { c a s e r e s u l t : = < - c : r e s u l t s = a p p e n d ( r e s u l t s , r e s u l t ) c a s e < - t i m e o u t : f m t . P r i n t l n ( " t i m e d o u t " ) r e t u r n } } r e t u r n Run
  11. Build-in tools go doc (documentation of any package) go get

    (fetch dependencies) go fmt (formats to the coding standard) go test (run all tests) and many more
  12. Google app engine a p p l i c a

    t i o n : h e l l o w o r l d v e r s i o n : 1 r u n t i m e : g o a p i _ v e r s i o n : g o 1 h a n d l e r s : - u r l : / . * s c r i p t : _ g o _ a p p
  13. Google app engine with scaling a p p l i

    c a t i o n : h e l l o w o r l d v e r s i o n : 1 r u n t i m e : g o a p i _ v e r s i o n : g o 1 h a n d l e r s : - u r l : / . * s c r i p t : _ g o _ a p p a u t o m a t i c _ s c a l i n g : m i n _ i d l e _ i n s t a n c e s : 5 m a x _ i d l e _ i n s t a n c e s : a u t o m a t i c # d e f a u l t v a l u e m i n _ p e n d i n g _ l a t e n c y : 3 0 m s # d e f a u l t v a l u e m a x _ p e n d i n g _ l a t e n c y : a u t o m a t i c m a x _ c o n c u r r e n t _ r e q u e s t s : 5 0