Golang from Scala developer’s perspective

52
Golang from Scala developer’s perspective

Transcript of Golang from Scala developer’s perspective

Page 1: Golang from Scala developer’s perspective

Golang from  Scala  developer’s  perspective

Page 2: Golang from Scala developer’s perspective

About  me

Svetlana  BozhkoBackend  Scala  Developer  @  Adform

Page 3: Golang from Scala developer’s perspective

The  goalIs  Go  really  so  bad imperative?

Page 4: Golang from Scala developer’s perspective

Content

Short  intro  into  GoTooling  FP  vs.  GoIMHO

Page 5: Golang from Scala developer’s perspective

Usagehttps://github.com/golang/go/wiki/GoUsers

Page 6: Golang from Scala developer’s perspective
Page 7: Golang from Scala developer’s perspective

How  to  start?http://tour.golang.org/http://golangshow.com/

Page 8: Golang from Scala developer’s perspective

GoGood  parts

Page 9: Golang from Scala developer’s perspective

Go  is  actually  a  pretty  simple  language!

Page 10: Golang from Scala developer’s perspective

Even  too  simple  for  2015

Page 11: Golang from Scala developer’s perspective
Page 12: Golang from Scala developer’s perspective

Advanced  GC

Page 13: Golang from Scala developer’s perspective

“Build  time  matters  more  than  anything  else”  -­‐ Rob  Pike

Page 14: Golang from Scala developer’s perspective

gofmt

Page 15: Golang from Scala developer’s perspective

Quite  good  toolsbuild-­‐in  (version,  get,  build,  …)editors  (vim,  intellij,  sublime,  …)

Page 16: Golang from Scala developer’s perspective

Concurrency• Channels

send  data  between  threads  easily,  asynchronously  or  synchronously

• GoRoutinesspin  up  another  thread,  just  like  that!

Page 17: Golang from Scala developer’s perspective

No  Language  Is  Perfect...

Page 18: Golang from Scala developer’s perspective

Go  Mentality• Smallest  possible  feature  set• It’s  okay  to  copy  some  code  

• Productivity  instead  of  hyper-­‐elegant  code

Page 19: Golang from Scala developer’s perspective

Let’s  get  rid  of• Implicit  numeric  conversion  (int !=  int32)• ‘Implements’  keyword• ‘Classes’• Constructor/destructor• Function  overloading• Pointer  arithmetic  • Exceptions  and  try/catch• Generics• …

Page 20: Golang from Scala developer’s perspective

Strict  compilerwhich  interrupts  you  sometimes

Page 21: Golang from Scala developer’s perspective

Wait,  Go  has  ‘goto’?

Page 22: Golang from Scala developer’s perspective

Wait,  how  to  debug?GDB,  but  it’s  not  very  good  way

Page 23: Golang from Scala developer’s perspective

Dependency  management

vendor  (since  1.5)  

Page 24: Golang from Scala developer’s perspective

FP  vs.  Go

Page 25: Golang from Scala developer’s perspective

Wikipedia:“Functional  programming is  a programming   paradigm  — a  style  of  building  the  structure  and  elements  of  computer  programs  — that  treats computation as  the  evaluation  of mathematical   functions and  avoids  changing-­‐state and mutable data.”  

Page 26: Golang from Scala developer’s perspective

What  it  means?No  mutable  data  (no  side  effect)

No  mutable  state  (no  implicit,  hidden  state)Same  result  returned  by  functions  called  with  the  same  inputs

Functions  are  pure  functions  in  the  mathematical  sense

Page 27: Golang from Scala developer’s perspective

Advantages?

Cleaner  codeReferential  transparency

Page 28: Golang from Scala developer’s perspective

Referential  transparencyMemoizationCache  resultsIdempotenceModularizationWe  have  no  stateEase  of  debugging

Functions  are  isolated  and  very  easy  to  debug

Page 29: Golang from Scala developer’s perspective

Referential  transparency

ParallelizationFunctions  calls  are  independent  

We  can  parallelize   in  different  process/CPUs/computers/...  

res  =  func1(a,  b)  +  func2(a,  c)

Page 30: Golang from Scala developer’s perspective

Referential  transparency

With  no  shared  data,  concurrency gets  a  lot  simpler:No  semaphoresNo  monitors  No  locks

No  race-­‐conditionsNo  dead-­‐locks

Page 31: Golang from Scala developer’s perspective

NonethelessGolang has  built-­‐in  “sync”  package  with  mutexes

Page 32: Golang from Scala developer’s perspective

Don’t  Update,  Create!

Page 33: Golang from Scala developer’s perspective

String

Page 34: Golang from Scala developer’s perspective

Arrays

Page 35: Golang from Scala developer’s perspective

Maps

Page 36: Golang from Scala developer’s perspective

Higher  order  functions

Page 37: Golang from Scala developer’s perspective

FP  librarieshttps://github.com/yanatan16/itertools

https://github.com/tobyhede/go-­‐underscore

Page 38: Golang from Scala developer’s perspective

Higher  order  functions

Page 39: Golang from Scala developer’s perspective

Map

Page 40: Golang from Scala developer’s perspective

Filter

Page 41: Golang from Scala developer’s perspective

Reduce

Page 42: Golang from Scala developer’s perspective

Closures

Page 43: Golang from Scala developer’s perspective

Currying  and  Partial  Functions

Page 44: Golang from Scala developer’s perspective

Eager  vs  Lazy  Evaluation

Golang channels  and  goroutines enable  the  creation  of  generators  that  could  be  a  way  to  have  

lazy  evaluation

Page 45: Golang from Scala developer’s perspective

Recursion

Page 46: Golang from Scala developer’s perspective

FP  &  OOP

It  is  possible  do  FP  in  OOP?  Yes  it  is!

OOP  is  orthogonal  to  FP.  At  least  in  theory…

Page 47: Golang from Scala developer’s perspective

Exercise!

What  is  the  sum  of  the  first  10  natural  number  whose  square  value  is  divisible  by  2?

Page 48: Golang from Scala developer’s perspective

Exercise!Imperative Functional

Page 49: Golang from Scala developer’s perspective

Learn  at  least  one  functional  language

It  will  open  your  mind  to  a  new  paradigm  becoming  you  a  better  programmer

Page 50: Golang from Scala developer’s perspective

Conclusion

Technologies  are  awesome!  The  problem  is  people how  we  use  them.

Page 51: Golang from Scala developer’s perspective

Thank  you!Questions?

Svetlana  Bozhko aka  @SBozhkohttp://devzen.ru/

[email protected]

Page 52: Golang from Scala developer’s perspective

Links

https://golang-­‐ru.slack.com/messageshttps://www.youtube.com/watch?list=PLPHSBhlVt

TyfwIKn7r_a5xkzzMu-­‐iey-­‐w&v=cGXorQkw3JE