SlideShare ist ein Scribd-Unternehmen logo
1 von 34
Downloaden Sie, um offline zu lesen
13 
December 
2014 
getting started with 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim 
Go 
Programming 
Language 
Abiola 
Ibrahim 
@abiosoB
13 
December 
2014 
AGENDA 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim 
• My 
Go 
Story 
• IntroducGon 
to 
Go 
• Build 
our 
applicaGon 
• Go 
on 
App 
Engine 
• AdopGon 
of 
Go 
• Useful 
Links
13 
December 
2014 
My 
Go 
Story 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim 
• 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.
Real 
World 
Go 
talk 
-­‐ 
Andrew 
Gerrand, 
2011. 
13 
December 
2014 
Why 
Go 
? 
“ 
“Speed, 
reliability, 
or 
simplicity: 
pick 
two.” 
(some9mes 
just 
one). 
Can’t 
we 
do 
be@er? 
” 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim
13 
December 
2014 
Scope 
of 
Talk 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim 
• 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
INTRODUCTION 
TO 
GO 
13 
December 
2014 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim
Go 
is 
staGcally 
typed, 
but 
type 
inference 
saves 
repeGGon. 
13 
December 
2014 
Simple 
Type 
System 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim 
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)
Statements 
are 
terminated 
with 
semicolons 
but 
you 
don’t 
need 
to 
include 
it. 
var a int = 1 // valid line 
var b string = “GDays”; // also valid line 
13 
December 
2014 
Syntax 
and 
Structure 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim
iota 
can 
come 
handy 
in 
constants 
const EVENT = “GDays” //value cannot change 
const ( 
ANDROID_SESSION = iota // 0 
GOLANG_SESSION // 1 
GIT_SESSION // 2 
13 
December 
2014 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim 
)
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”) 
}
No 
while, 
no 
do 
while, 
just 
for. 
for i < 10 { ... } //while loop in other languages 
for { ... } //infinite loop 
// range over arrays, slices and maps 
nums := [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 
}
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) 
}
var nums [4]int //array declaration 
nums := [4]int{0, 1, 2, 3} //declare and initialize 
n := nums[0:2] // slice of nums from index 0 to 1 
n := nums[:2] // same as above 
n := nums[2:] // from index 2 to max index 
n := nums[:] // slice of entire array 
nums[0] = 1 // assignment 
n[0] = 2 // assignment 
var copyOfNum = make([]int, 4) //slice allocation 
copy(copyOfNum, nums[:]) //copy nums into it 
13 
December 
2014 
Arrays 
and 
Slices 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim
var gdays map[string]string //declaration 
gdays = make(map[string]string) //allocation 
//shorter declaration with allocation 
var gdays = make(map[string]string) 
gdays := make(map[string]string) 
gdays[“location”] = “Chams City” //assignment 
//declaration with values 
gdays := map[string]string { 
“location” : “Chams City” 
13 
December 
2014 
Maps 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim 
}
13 
December 
2014 
Structs 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim 
type Point struct { 
X int, 
Y int, 
Z int, 
} 
var p = &Point{0, 1, 3} 
//partial initialization 
var p = &Point{ 
X: 1, 
Z: 2, 
}
//void function without return value 
func SayHelloToMe(name string){ 
fmt.Printf(”Hello, %s”, name) 
} 
//function with return value 
func Multiply(x, y int) int { 
13 
December 
2014 
FuncGons 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim 
return x * y 
}
13 
December 
2014 
Methods 
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 Rectangle 
r.Area() // == 12
Types 
and 
Methods 
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 
13 
December 
2014 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim
13 
December 
2014 
Scoping 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim 
• 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 
Visibility 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim 
• No 
use 
of 
private 
or 
public 
keywords. 
• Visibility 
is 
defined 
by 
case 
//visible to other packages 
var Name string = “” 
func Hello(name string) string { 
return fmt.Printf(“Hello %s”, name) 
} 
//not visible to other packages 
var name string = “” 
func hello(name string) string { 
return fmt.Printf(“Hello %s”, name) 
}
13 
December 
2014 
Concurrency 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim 
• GorouGnes 
are 
like 
threads 
but 
cheaper. 
MulGple 
per 
threads. 
• CommunicaGons 
between 
gorouGnes 
are 
done 
with 
channels 
done := make(chan bool) //create channel 
doSort := 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
BUILDING 
OUR 
APPLICATION 
13 
December 
2014 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim
Download 
Go 
installer 
for 
your 
pladorm 
at 
hep://golang.org/doc/install 
13 
December 
2014 
InstallaGon 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim
13 
December 
2014 
Code 
Session 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim 
• 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
GO 
ON 
APP 
ENGINE 
13 
December 
2014 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim
13 
December 
2014 
Code 
Session 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim 
• 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
ADOPTION 
OF 
GO 
13 
December 
2014 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim
13 
December 
2014 
Go 
in 
ProducGon 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim 
• 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 
Community 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim 
• 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 
– abiola89@gmail.com
13 
December 
2014 
THANK 
YOU 
Ge.ng 
Started 
with 
Go 
| 
Abiola 
Ibrahim

Weitere ähnliche Inhalte

Ähnlich wie Getting started with Go at GDays Nigeria 2014

eXo EC - Groovy Programming Language
eXo EC - Groovy Programming LanguageeXo EC - Groovy Programming Language
eXo EC - Groovy Programming Language
Hoat Le
 

Ähnlich wie Getting started with Go at GDays Nigeria 2014 (20)

An Introduction to Groovy for Java Developers
An Introduction to Groovy for Java DevelopersAn Introduction to Groovy for Java Developers
An Introduction to Groovy for Java Developers
 
Go for Mobile Games
Go for Mobile GamesGo for Mobile Games
Go for Mobile Games
 
go language- haseeb.pptx
go language- haseeb.pptxgo language- haseeb.pptx
go language- haseeb.pptx
 
Go. Why it goes
Go. Why it goesGo. Why it goes
Go. Why it goes
 
eXo EC - Groovy Programming Language
eXo EC - Groovy Programming LanguageeXo EC - Groovy Programming Language
eXo EC - Groovy Programming Language
 
JLPDevs - Optimization Tooling for Modern Web App Development
JLPDevs - Optimization Tooling for Modern Web App DevelopmentJLPDevs - Optimization Tooling for Modern Web App Development
JLPDevs - Optimization Tooling for Modern Web App Development
 
Groovy And Grails Introduction
Groovy And Grails IntroductionGroovy And Grails Introduction
Groovy And Grails Introduction
 
Getting started with go - Florin Patan - Codemotion Milan 2016
Getting started with go - Florin Patan - Codemotion Milan 2016Getting started with go - Florin Patan - Codemotion Milan 2016
Getting started with go - Florin Patan - Codemotion Milan 2016
 
Me&g@home
Me&g@home Me&g@home
Me&g@home
 
Google... more than just a cloud
Google... more than just a cloudGoogle... more than just a cloud
Google... more than just a cloud
 
Groovy for Java Developers
Groovy for Java DevelopersGroovy for Java Developers
Groovy for Java Developers
 
Influx/Days 2017 San Francisco | Dan Vanderkam
Influx/Days 2017 San Francisco | Dan VanderkamInflux/Days 2017 San Francisco | Dan Vanderkam
Influx/Days 2017 San Francisco | Dan Vanderkam
 
Golang introduction
Golang introductionGolang introduction
Golang introduction
 
Pear Deck
Pear DeckPear Deck
Pear Deck
 
Recommender Systems with Ruby (adding machine learning, statistics, etc)
Recommender Systems with Ruby (adding machine learning, statistics, etc)Recommender Systems with Ruby (adding machine learning, statistics, etc)
Recommender Systems with Ruby (adding machine learning, statistics, etc)
 
Groovy intro
Groovy introGroovy intro
Groovy intro
 
BDD: Behind the Scenes
BDD: Behind the ScenesBDD: Behind the Scenes
BDD: Behind the Scenes
 
Go programming introduction
Go programming introductionGo programming introduction
Go programming introduction
 
Golang preso
Golang presoGolang preso
Golang preso
 
Eclipsecon08 Introduction To Groovy
Eclipsecon08 Introduction To GroovyEclipsecon08 Introduction To Groovy
Eclipsecon08 Introduction To Groovy
 

Kürzlich hochgeladen

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Kürzlich hochgeladen (20)

HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 

Getting started with Go at GDays Nigeria 2014

  • 1. 13 December 2014 getting started with Ge.ng Started with Go | Abiola Ibrahim Go Programming Language Abiola Ibrahim @abiosoB
  • 2. 13 December 2014 AGENDA Ge.ng Started with Go | Abiola Ibrahim • My Go Story • IntroducGon to Go • Build our applicaGon • Go on App Engine • AdopGon of Go • Useful Links
  • 3. 13 December 2014 My Go Story Ge.ng Started with Go | Abiola Ibrahim • 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.
  • 4. Real World Go talk -­‐ Andrew Gerrand, 2011. 13 December 2014 Why Go ? “ “Speed, reliability, or simplicity: pick two.” (some9mes just one). Can’t we do be@er? ” Ge.ng Started with Go | Abiola Ibrahim
  • 5. 13 December 2014 Scope of Talk Ge.ng Started with Go | Abiola Ibrahim • 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
  • 6. INTRODUCTION TO GO 13 December 2014 Ge.ng Started with Go | Abiola Ibrahim
  • 7. Go is staGcally typed, but type inference saves repeGGon. 13 December 2014 Simple Type System Ge.ng Started with Go | Abiola Ibrahim 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)
  • 8. Statements are terminated with semicolons but you don’t need to include it. var a int = 1 // valid line var b string = “GDays”; // also valid line 13 December 2014 Syntax and Structure Ge.ng Started with Go | Abiola Ibrahim
  • 9. iota can come handy in constants const EVENT = “GDays” //value cannot change const ( ANDROID_SESSION = iota // 0 GOLANG_SESSION // 1 GIT_SESSION // 2 13 December 2014 Ge.ng Started with Go | Abiola Ibrahim )
  • 10. 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”) }
  • 11. No while, no do while, just for. for i < 10 { ... } //while loop in other languages for { ... } //infinite loop // range over arrays, slices and maps nums := [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 }
  • 12. 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 }
  • 13. 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 }
  • 14. 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) }
  • 15. var nums [4]int //array declaration nums := [4]int{0, 1, 2, 3} //declare and initialize n := nums[0:2] // slice of nums from index 0 to 1 n := nums[:2] // same as above n := nums[2:] // from index 2 to max index n := nums[:] // slice of entire array nums[0] = 1 // assignment n[0] = 2 // assignment var copyOfNum = make([]int, 4) //slice allocation copy(copyOfNum, nums[:]) //copy nums into it 13 December 2014 Arrays and Slices Ge.ng Started with Go | Abiola Ibrahim
  • 16. var gdays map[string]string //declaration gdays = make(map[string]string) //allocation //shorter declaration with allocation var gdays = make(map[string]string) gdays := make(map[string]string) gdays[“location”] = “Chams City” //assignment //declaration with values gdays := map[string]string { “location” : “Chams City” 13 December 2014 Maps Ge.ng Started with Go | Abiola Ibrahim }
  • 17. 13 December 2014 Structs Ge.ng Started with Go | Abiola Ibrahim type Point struct { X int, Y int, Z int, } var p = &Point{0, 1, 3} //partial initialization var p = &Point{ X: 1, Z: 2, }
  • 18. //void function without return value func SayHelloToMe(name string){ fmt.Printf(”Hello, %s”, name) } //function with return value func Multiply(x, y int) int { 13 December 2014 FuncGons Ge.ng Started with Go | Abiola Ibrahim return x * y }
  • 19. 13 December 2014 Methods 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 Rectangle r.Area() // == 12
  • 20. Types and Methods 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 13 December 2014 Ge.ng Started with Go | Abiola Ibrahim
  • 21. 13 December 2014 Scoping Ge.ng Started with Go | Abiola Ibrahim • Package level scope • No classes • A package can have mulGple source files • Package source files reside in same directory package main // sample package declaration
  • 22. 13 December 2014 Visibility Ge.ng Started with Go | Abiola Ibrahim • No use of private or public keywords. • Visibility is defined by case //visible to other packages var Name string = “” func Hello(name string) string { return fmt.Printf(“Hello %s”, name) } //not visible to other packages var name string = “” func hello(name string) string { return fmt.Printf(“Hello %s”, name) }
  • 23. 13 December 2014 Concurrency Ge.ng Started with Go | Abiola Ibrahim • GorouGnes are like threads but cheaper. MulGple per threads. • CommunicaGons between gorouGnes are done with channels done := make(chan bool) //create channel doSort := 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
  • 24. BUILDING OUR APPLICATION 13 December 2014 Ge.ng Started with Go | Abiola Ibrahim
  • 25. Download Go installer for your pladorm at hep://golang.org/doc/install 13 December 2014 InstallaGon Ge.ng Started with Go | Abiola Ibrahim
  • 26. 13 December 2014 Code Session Ge.ng Started with Go | Abiola Ibrahim • 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
  • 27. GO ON APP ENGINE 13 December 2014 Ge.ng Started with Go | Abiola Ibrahim
  • 28. 13 December 2014 Code Session Ge.ng Started with Go | Abiola Ibrahim • 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
  • 29. ADOPTION OF GO 13 December 2014 Ge.ng Started with Go | Abiola Ibrahim
  • 30. 13 December 2014 Go in ProducGon Ge.ng Started with Go | Abiola Ibrahim • Google • Canonical • DropBox • SoundCloud • Heroku • Digital Ocean • Docker And many more at hep://golang.org/wiki/GoUsers
  • 31. 13 December 2014 Ge.ng Started with Go | Abiola Ibrahim USEFUL LINKS
  • 32. 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
  • 33. 13 December 2014 Community Ge.ng Started with Go | Abiola Ibrahim • 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 – abiola89@gmail.com
  • 34. 13 December 2014 THANK YOU Ge.ng Started with Go | Abiola Ibrahim