34
13 December 2014 Ge.ng Started with Go | Abiola Ibrahim getting started with Go Programming Language Abiola Ibrahim @ abiosoB

Getting started with Go at GDays Nigeria 2014

Embed Size (px)

Citation preview

Page 1: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

getting started with  

Go  Programming  Language  

Abiola  Ibrahim  @abiosoB  

Page 2: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

AGENDA  

•  My  Go  Story  •  IntroducGon  to  Go  •  Build  our  applicaGon  •  Go  on  App  Engine  •  AdopGon  of  Go  •  Useful  Links  

Page 3: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

My  Go  Story  

•  Interpreted  Languages  have  slower  runGmes  and  no  compile  Gme  error  checks.  

•  I  like  Java  but  not  the  JVM  overhead.  •  Building  C++  is  not  as  fun  as  desired.  •  Go  is  compiled,  single  binary  (no  dependencies)  and  builds  very  fast.  

Page 4: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

Why  Go  ?  

 “  “Speed,  reliability,  or  simplicity:  pick  

two.”  (some9mes  just  one).      

Can’t  we  do  be@er?  ”      Real  World  Go  talk  -­‐  Andrew  Gerrand,  2011.    

Page 5: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

Scope  of  Talk  

•  Things  this  talk  cannot  help  you  do  – Build  Drones  – Create  Google.com  killer  – Replicate  Facebook    

•  Things  this  talk  can  help  you  do  – Understand  the  core  of  Go  – Start  wriGng  Go  apps  

Page 6: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

INTRODUCTION  TO  GO  

Page 7: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

Simple  Type  System  Go  is  staGcally  typed,  but  type  inference  saves  repeGGon.      

Java/C/C++:    int i = 1;

Go:        i := 1 // type int pi := 3.142 // type float64 hello := "Hello, GDays!" // type string add := func(x, y int) int { return x + y }

//type func (x, y int)

Page 8: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

Syntax  and  Structure  Statements  are  terminated  with  semicolons  but  you  don’t  need  to  include  it.  

var a int = 1 // valid linevar b string = “GDays”; // also valid line

Page 9: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

iota  can  come  handy  in  constants  

const EVENT = “GDays” //value cannot change

const (ANDROID_SESSION = iota // 0GOLANG_SESSION // 1GIT_SESSION // 2

)

Page 10: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

No  brackets,  just  braces.  

if a < 0 {fmt.Printf(“%d is negative”, a)

}

for i := 0; i< 10; i++ {fmt.Println(“GDays is awesome”)

}

Page 11: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

No  while,  no  do  while,  just  for.    

for i < 10 { ... } //while loop in other languages

for { ... } //infinite loop

// range over arrays, slices and mapsnums := [4]int{0, 1, 2, 3}for i, value := range nums {

fmt.Println(“value at %d is %d”, i, value)}

gdays := map[string]string{ “loc” : “Chams City”, ... }for key, value := range gdays {

fmt.Println(“value at %s is %s”, key, value)}

Page 12: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

If  statement  supports  iniGalizaGon.  

file, err := os.Open(“sample.txt”)if err == nil {

fmt.Println(file.Name())}

This  can  be  shortened  to  

if file, err := os.Open(“sample.txt”); err == nil {fmt.Println(file.Name())

}

Page 13: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

MulGple  return  values.  

func Sqrt(float64 n) (float64, err) {if (n < 0){

return 0, errors.New(“Negative number”)}return math.Sqrt(n), nil

}

Page 14: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

MulGple  assignments.  

a, b, c := 1, 2, 3

func Reverse(str string) string {b := []byte(str)for i, j := 0, len(b)-1; i != j; i, j = i+1, j-1 {

b[i], b[j] = b[j], b[i]}return string(b)

}

Page 15: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

Arrays  and  Slices  var nums [4]int //array declarationnums := [4]int{0, 1, 2, 3} //declare and initialize

n := nums[0:2] // slice of nums from index 0 to 1n := nums[:2] // same as aboven := nums[2:] // from index 2 to max indexn := nums[:] // slice of entire array

nums[0] = 1 // assignmentn[0] = 2 // assignment

var copyOfNum = make([]int, 4) //slice allocationcopy(copyOfNum, nums[:]) //copy nums into it  

Page 16: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

Maps  var gdays map[string]string //declarationgdays = make(map[string]string) //allocation

//shorter declaration with allocationvar gdays = make(map[string]string)gdays := make(map[string]string)

gdays[“location”] = “Chams City” //assignment

//declaration with valuesgdays := map[string]string {

“location” : “Chams City”}  

Page 17: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

Structs  type Point struct {

X int,Y int,Z int,

}

var p = &Point{0, 1, 3}

//partial initializationvar p = &Point{

X: 1,Z: 2,

}

Page 18: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

FuncGons  //void function without return valuefunc SayHelloToMe(name string){

fmt.Printf(”Hello, %s”, name)}

//function with return valuefunc Multiply(x, y int) int {

return x * y}

Page 19: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

 type Rectangle struct {

X, Y int }

func (r Rectangle) Area() int {return r.X * r.Y

}

r := Rectangle{4, 3} // type Rectangler.Area() // == 12  

Methods  

Page 20: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

You  can  define  methods  on  any  type.  

type MyInt int

func (m MyInt) Square() int {i := int(m)return i * i

}

num := MyInt(4)num.Square() // == 16

Types  and  Methods  

Page 21: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

Scoping  

•  Package  level  scope  •  No  classes  •  A  package  can  have  mulGple  source  files  •  Package  source  files  reside  in  same  directory  

package main // sample package declaration

Page 22: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

Visibility  •  No  use  of  private  or  public  keywords.  •  Visibility  is  defined  by  case

//visible to other packagesvar Name string = “”func Hello(name string) string {

return fmt.Printf(“Hello %s”, name)}

//not visible to other packagesvar name string = “”func hello(name string) string {

return fmt.Printf(“Hello %s”, name)}  

Page 23: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

Concurrency  •  GorouGnes  are  like  threads  but  cheaper.  MulGple  per  threads.  •  CommunicaGons  between  gorouGnes  are  done  with  channels  

done := make(chan bool) //create channeldoSort := func(s []int) {

sort(s)done <- true //inform channel goroutine is done

}i := pivot(s)go doSort(s[:i]) go doSort(s[i:])<-done //wait for message on channel<-done //wait for message on channel  

Page 24: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

BUILDING  OUR  APPLICATION  

Page 25: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

InstallaGon  

Download  Go  installer  for  your  pladorm  at    hep://golang.org/doc/install    

Page 26: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

Code  Session  

•  Let  us  build  a  GDays  Aeendance  app.  •  Displays  total  number  of  aeendees  •  Ability  to  mark  yourself  as  present  

Source  code  will  be  available  aBer  session  at  hep://github.com/abiosoB/gdays-­‐ae    

Page 27: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

GO  ON  APP  ENGINE  

Page 28: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

Code  Session  

•  Let  us  modify  our  app  to  suit  App  Engine  – Use  User  service  to  uniquely  idenGfy  user  – Use  Data  Store  to  persist  number  of  aeendees  

Source  code  will  be  available  aBer  session  at  hep://github.com/abiosoB/gdays-­‐ae-­‐appengine        

Page 29: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

ADOPTION  OF  GO  

Page 30: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

Go  in  ProducGon  •  Google  •  Canonical  •  DropBox  •  SoundCloud  •  Heroku  •  Digital  Ocean  •  Docker  

And  many  more  at  hep://golang.org/wiki/GoUsers    

Page 31: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

USEFUL  LINKS  

Page 32: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

•  hep://golang.org/doc          -­‐  official  documentaGon  •  hep://tour.golang.org          -­‐  interacGve  tutorials  •  hep://gobyexample.com        -­‐  tutorials  •  hep://gophercasts.io          -­‐  screencasts  •  hep://golang.org/wiki/ArGcles    -­‐  lots  of  helpful  arGcles  

Page 33: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

Community  •  Blog  –  hep://blog.golang.org    

•  Google  Group  –  heps://groups.google.com/d/forum/golang-­‐nuts    

•  Gopher  Academy  –  hep://gopheracademy.com    

•  Me  –  hep://twieer.com/abiosoB    –  [email protected]    

Page 34: Getting started with Go at GDays Nigeria 2014

13  December  2014        Ge.ng  Started  with  Go  |  Abiola  Ibrahim  

THANK  YOU