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

GoSXBGo! - Introduction to the language

Soulou
September 18, 2015

GoSXBGo! - Introduction to the language

Introduction to the go programming language, a bit of background, several example.

Built for meetup event: http://www.meetup.com/fr/Go-SXB-Go/events/224878195/?eventId=224878195

Soulou

September 18, 2015
Tweet

More Decks by Soulou

Other Decks in Programming

Transcript

  1. Why this meetup Thousands of loc running in production to

    run our product. Working with Go for ± 30 months Lot of things to share about the language Create/Stimulate the local community Spread the language
  2. How this meetup will take place Introduction to the language

    Please make remarks, tro.. arguments about anything
  3. History 2007: Rob Pike (UTF­8, Plan 9), Ken Thompson (B,

    C) at Google 2009: First public release, BSD License The gopher ... again 2012: 1.0 Released August 2015: 1.5 Released
  4. Why this new language? Efficient large scale D o n

    ' t t h i n k l a r g e a p p s , b u t l a r g e t e a m s Distributed systems A n y a p p i s d i s t r i b u t e d t o d a y , m a k i n g r e q u e s t t o r e m o t e A P I ? Y O U R a p p i s d i s t r i b u t e d ! Multicore hardware M o o r ' s l a w i s n o t w o r k i n g a n y m o r e , p r o c e s s o r s a r e n o t b e t t e r , t h e y a r e p a r a l l e l Speed of compilation P r o b l e m a t G o o g l e , s o m e b i n a r i e s t a k e s e v e r a l h o u r s t o b u i l d
  5. Why using go? Concurrency Types Scalability Simplicity Fast development cycle

    Very readable Garbage collected Awesome tooling Much more...
  6. Respect the tradition UTF­8 Inside 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 e l l o w o r l d ! ☺ " ) } Run
  7. Example web server p a c k a g e

    m a i n i m p o r t ( " f m t " " l o g " " 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 ) { l o g . P r i n t l n ( " C o n n e c t i o n c o m i n g f r o m " , r . R e m o t e A d d r ) f m t . F p r i n t f ( w , " H e l l o W o r l d " ) } ) l o g . P r i n t l n ( " L i s t e n i n g o n p o r t 5 0 0 0 " ) h t t p . L i s t e n A n d S e r v e ( " : 5 0 0 0 " , n i l ) } Run
  8. Concurrency Model Communicating Sequential Processes 1978 Communicate by sharing memory

    → Not recommanded but possible Share memory by communicating → Go model
  9. Example (broken example) v a r ( i = 0

    ) f u n c n e x t I D ( ) i n t { i + = 1 r e t u r n i } f u n c m a i n ( ) { g o f u n c ( ) { f m t . P r i n t l n ( n e x t I D ( ) ) } ( ) f m t . P r i n t l n ( n e x t I D ( ) ) t i m e . S l e e p ( t i m e . S e c o n d ) } Question? What does it display?
  10. Example (broken example) v a r ( i = 0

    ) f u n c n e x t I D ( ) i n t { i + = 1 r e t u r n i } f u n c m a i n ( ) { g o f u n c ( ) { f m t . P r i n t l n ( n e x t I D ( ) ) } ( ) f m t . P r i n t l n ( n e x t I D ( ) ) t i m e . S l e e p ( t i m e . S e c o n d ) } Answer: unknown
  11. Examples (mutex, non idiomatic method) " f m t "

    v a r ( i = 0 m I D = s y n c . M u t e x { } ) f u n c n e x t I D ( ) i n t { m I D . L o c k ( ) i + = 1 m I D . U n l o c k ( ) r e t u r n i } f u n c m a i n ( ) { g o f u n c ( ) { f m t . P r i n t l n ( n e x t I D ( ) ) } ( ) f m t . P r i n t l n ( n e x t I D ( ) ) t i m e . S l e e p ( t i m e . S e c o n d ) }
  12. Examples (go method) v a r ( g e n

    = m a k e ( c h a n i n t ) ) f u n c i n i t ( ) { f o r i : = 0 ; ; i + + { g e n < - i } } f u n c m a i n ( ) { g o f u n c ( ) { f m t . P r i n t l n ( < - g e n ) } ( ) f m t . P r i n t l n ( < - g e n ) t i m e . S l e e p ( t i m e . S e c o n d ) }
  13. Examples (go method) v a r ( g e n

    = m a k e ( c h a n i n t ) ) f u n c i n i t ( ) { f o r i : = 0 ; ; i + + { g e n < - i } } f u n c m a i n ( ) { g o f u n c ( ) { f m t . P r i n t l n ( < - g e n ) } ( ) f m t . P r i n t l n ( < - g e n ) t i m e . S l e e p ( t i m e . S e c o n d ) }
  14. Type System Immutable UTF­8 string Immutable pointers, forget C pointer

    arithmetic Implicit interfaces Bits of reflectivity Garbage collected (not really part of the type system)
  15. Structures t y p e P o i n t

    s t r u c t { X , Y i n t s t a t e s t r i n g }
  16. Methods t y p e A n i m a

    l s t r u c t { / / . . . } f u n c ( a A n i m a l ) W a l k ( ) { / / D o s o m e t h i n g } f u n c m a i n ( ) { a : = A n i m a l { } a . W a l k ( ) }
  17. Interfaces t y p e W r i t e

    r i n t e r f a c e { W r i t e ( [ ] b y t e ) ( i n t , e r r o r ) }
  18. Interfaces example p a c k a g e m

    a i n i m p o r t ( " f m t " " i o " " o s " ) f u n c w r i t e T o F i l e ( c o n t e n t s t r i n g ) e r r o r { f d , e r r : = o s . O p e n ( " / t m p / t o t o " ) i f e r r ! = n i l { r e t u r n e r r } n , e r r : = f d . W r i t e ( [ ] b y t e ( c o n t e n t ) ) } f u n c m a i n ( ) { w r i t e T o F i l e ( " t o t o " ) } Run
  19. At one condition Defined directory structure $ G O P

    A T H / b i n / p r o g r a m 1 p k g / l i n u x _ a m d 6 4 / g i t h u b . c o m / S o u l o u p a c k a g e 1 . a s r c / g i t h u b . c o m / S o u l o u / p a c k a g e 1 / l i b . g o p r o g r a m 1 / m a i n . g o s p o t d . g o
  20. The go tool g o g e t Get a

    remote package and install it locally g o r u n Build and run a go program (script­like) g o b u i l d Build the package/program g o i n s t a l l Build the program and put it in $GOPATH/bin g o t e s t Run the tests of your project
  21. One format to rule them all g o f m

    t Format your code to respect go conventions 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 e l l o " ) } Becomes 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 e l l o " ) }
  22. Import paths All import paths are relative to your GOPATH

    $ G O P A T H s r c / g i t h u b . c o m / S o u l o u / p a c k a g e 1 In the gocode requiring this package: i m p o r t ( " g i t h u b . c o m / S o u l o u / p a c k a g e 1 " ) f u n c m a i n ( ) { p a c k a g e 1 . M y F u n c t i o n ( ) }
  23. Remote dependencies If a program requires the following package i

    m p o r t " g o l a n g . o r g / x / n e t / w e b s o c k e t " Fetching, building and installing a package is one command away $ g o g e t g o l a n g . o r g / x / n e t / w e b s o c k e t That's it your system is read
  24. Dependency managements The Go team has willingly not embedded any

    system L e t m e r e m i n d y o u t h i s b e f o r e a n y t r o l l i n g : * R u b y g e m s i s e x t e r n a l t o t h e c o r e R u b y p r o j e c t * N P M i s e x t e r n a l t o t h e c o r e N o d e J S p r o j e c t * M a v e n i s e x t e r n a l t o t h e c o r e J a v a p r o j e c t Let the community build tools, and we'll see. Stop repeating “They have learnt nothing”
  25. Small parenthesis about Go 1.5 and vendoring If you have

    the following packages: $ G O P A T H / s r c / g i t h u b . c o m / S o u l o u / p r o g r a m 1 / v e n d o r / g o l a n g . o r g / x / n e t / w e b s o c k e t g o l a n g . o r g / x / n e t / w e b s o c k e t And you require r e q u i r e " g o l a n g . o r g / x / n e t / w e b s o c k e t " The one in the directory vendor of your project will be used.
  26. My personal combo Godeps + Gopkg.in Godeps Build by Keith

    Rarick (ex­Heroku) Handles the vendored dependencies of your project. Gopkg.in Built by Gustavo Niemeyer (Canonical) Simplify semver usage with Go
  27. Godeps g o d e p s a v e

    Vendor all the dependencies in Godeps/_workspace Don't update any, remove unused, add new g o d e p u p d a t e < p a c k a g e n a m e > Update a precise package by upding project version by global g o d e p g o b u i l d / t e s t / r u n / i n s t a l l Wrapper around the Go tools which adds Godeps/_workspace in $GOPATH (will be deprecated when Go official vendoring will be considered stable)
  28. Gopkg.in Wrapper over github to use tags github.com/nsqio/go­nsq (https://github.com/nsqio/go­nsq) i

    m p o r t " g o k p g . i o / n s q i o / n s q . v 0 " Different from i m p o r t " g o k p g . i o / n s q i o / n s q . v 1 " Want to update on your branch g o g e t - u g o p k g . i o / n s q i o / n s q . v 0 g o d e p u p d a t e g o p k g . i n / n s q i o / n s q . v 0
  29. Environment variable magic G O O S = l i

    n u x G O A R C H = a r m G O A R M = 7 g o b u i l d p r o g r a m 1 $ f i l e p r o g r a m 1 p r o g r a m 1 : E L F 3 2 - b i t L S B e x e c u t a b l e , A R M , E A B I 5 v e r s i o n 1 ( S Y S V ) , s t a t i c a l l y l i n k e d , n o t s t r i p p e d GOOS l i n u x d a r w i n w i n d o w s f r e e b s d s o l a r i s a n d r o i d i o s . . . GOARCH 3 8 6 a m d 6 4 a r m a r m 6 4 p p c 6 4 golang.org/doc/install/source (https://golang.org/doc/install/source)
  30. C Compatibility You want to use a C library? p

    a c k a g e m a i n / / # i n c l u d e < s t d i o . h > i m p o r t " C " f u n c m a i n ( ) { s : = C . C S t r i n g ( " H e l l o w o r l d " ) C . p u t s ( s ) } golang.org/cmd/cgo/ (https://golang.org/cmd/cgo/)
  31. Who's using it? Google, obviously Cloudflare, awesome blog: blog.cloudflare.com (https://blog.cloudflare.com)

    Canonical, flag product: juju github.com/juju/juju (https://github.com/juju/juju)
  32. Who's using it? (more) Apple, they are hiring go devs

    jobs.apple.com/us/search (https://jobs.apple.com/us/search) And many, many, many more. Every big vendor is using Go today. Twitter, Docker, Facebook, Amazon, IBM, Uber, Tumblr, Mozilla, MongoDB, Dropbox, etc. github.com/golang/go/wiki/GoUsers (https://github.com/golang/go/wiki/GoUsers) Look at their Github account
  33. A few resources A guide to learn the language [interactive

    website] (http://tour.golang.org) Concurrency is not Parallelism [slides] (https://talks.golang.org/2012/waza.slide) Go Concurrency Patterns [slides] (https://talks.golang.org/2012/concurrency.slide) Advanced Go Concurrency Patterns [slides] (https://talks.golang.org/2013/advconc.slide) The official go blog [website] (https://blog.golang.org) Godoc: documentation of all the public packages [website] (https://godoc.org)
  34. Credits Gopher images by Renee French under Creative Commons Attributions

    3.0 More gopher images here (https://blog.golang.org/gopher)