Getting started with Go at GDays Nigeria 2014
-
Upload
abiola-ibrahim -
Category
Software
-
view
442 -
download
3
Transcript of 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
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
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.
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.
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
13 December 2014 Ge.ng Started with Go | Abiola Ibrahim
INTRODUCTION TO GO
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)
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
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
)
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”)
}
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)}
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())
}
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
}
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)
}
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
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”}
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,
}
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}
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
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
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
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)}
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
13 December 2014 Ge.ng Started with Go | Abiola Ibrahim
BUILDING OUR APPLICATION
13 December 2014 Ge.ng Started with Go | Abiola Ibrahim
InstallaGon
Download Go installer for your pladorm at hep://golang.org/doc/install
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
13 December 2014 Ge.ng Started with Go | Abiola Ibrahim
GO ON APP ENGINE
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
13 December 2014 Ge.ng Started with Go | Abiola Ibrahim
ADOPTION OF GO
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
13 December 2014 Ge.ng Started with Go | Abiola Ibrahim
USEFUL LINKS
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
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]
13 December 2014 Ge.ng Started with Go | Abiola Ibrahim
THANK YOU