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
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