SlideShare a Scribd company logo
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 1/54
Go - Introduction
October 2017
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 2/54
Go
package main
import "fmt"
func main() {
fmt.Println("Go - Introductionnn")
for _, speaker := range []string{"Tomasz Smelcerz", "Piotr Miśkiewicz", "Piotr Mścichowski"} {
fmt.Println(speaker)
}
} Run
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 3/54
Speakers
Piotr Miśkiewicz
piotr.miskiewicz (at) sap.com
Piotr Mścichowski
piotr.mscichowski (at) hybris.com
Tomasz Smelcerz
tomasz.smelcerz (at) hybris.com
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 4/54
Brought to you by Hack Your Career
www.facebook.com/Hack.your.Career(https://www.facebook.com/Hack.your.Career)
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 5/54
Agenda
Go key concepts
Where to use Go
Code examples - part 1
Why Go was created
Who's using Go
Code examples - part 2
Summary
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 6/54
What's in it for me ?
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 7/54
What's in it for me ?
insights.stackover ow.com/survey/2017#top-paying-technologies(https://insights.stackover ow.com/survey/2017#top-
paying-technologies)
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 8/54
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 9/54
What's in it for me ?
insights.stackover ow.com/survey/2017#most-loved-dreaded-and-wanted
(https://insights.stackover ow.com/survey/2017#most-loved-dreaded-and-wanted)
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 10/54
Golang key concepts
Easy syntax
Statically typed
Duck typing
Concurrency
Garbage Collector on board
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 11/54
What for ?
For applications where high performance matters
OS applications
Network applications
Backend applications
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 12/54
Go examples - part 1
We'll just give you an overview of the most important features
For more detailed info, see:
- tour.golang.org/welcome/1(https://tour.golang.org/welcome/1)
- gobyexample.com(https://gobyexample.com)
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 13/54
Functions
1 package main
2
3 import (
4 "fmt"
5 "strings"
6 )
7
8 func printUpper(message string) {
9 fmt.Println(strings.ToUpper(message))
10 }
11
12 func main() {
13 printUpper("Golang")
14 } Run
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 14/54
First-Class Functions
1 package main
2
3 import "fmt"
4
5 func intSeq() func() int {
6 i := 0
7 return func() int {
8 i += 1
9 return i
10 }
11 }
12
13 func main() {
14
15 nextInt := intSeq()
16
17 fmt.Println(nextInt())
18 fmt.Println(nextInt())
19 fmt.Println(nextInt())
20
21 newInts := intSeq()
22 fmt.Println(newInts())
23 } Run
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 15/54
Multiple return values
1 package main
2
3 import "fmt"
4
5 func swap(a, b int) (int, int) {
6 return b, a
7 }
8
9 func main() {
10 x, y := swap(1, 2)
11
12 fmt.Println(x)
13 fmt.Println(y)
14 } Run
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 16/54
Error Handling
1 package main
2
3 import (
4 "fmt"
5 "strconv"
6 "strings"
7 )
8
9 func strToInt(s string) (int, error) {
10 s = strings.Trim(s, " ")
11 val, err := strconv.Atoi(s)
12 return val, err
13 }
14
15 func main() {
16 val, err := strToInt(" 45 ")
17 if err != nil {
18 fmt.Printf("Error: %s", err.Error())
19 return
20 }
21 fmt.Println(val)
22 } Run
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 17/54
Error Handling
The error type is an interface type.
An error variable represents any value that can describe itself as a string.
Here is the interface's declaration:
type error interface {
Error() string
}
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 18/54
Interfaces
Interfaces are named collections of method signatures.
We Use them to de ne contracts (like in Java)
We can implement an interface on objects of other types (usually structs)
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 19/54
Duck Typing in Go
"If it walks like a duck, and it quacks like a duck, then it is a duck"
No need to declare that a type implements an interface
All you need is to implement all the methods of the interface (on some type)
In go structs, simple types and even function types can implement interfaces
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 20/54
Interface implementation example
8 //start
9 type shape interface {
10 Area() float64
11 }
12
13 func describe(s shape) {
14 fmt.Println("My area is ", s.Area())
15 }
16
17 type circle struct {
18 radius float64
19 }
20
21 //This is how we define method on a struct
22 func (c circle) Area() float64 {
23 return math.Pi * c.radius * c.radius
24 }
25
26 func main() {
27 describe(circle{radius: 10})
28 }
29
30 //end Run
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 21/54
More on structs
1 package main
2
3 import "fmt"
4
5 type person struct {
6 FirstName string
7 LastName string
8 Age int
9 }
10
11 //Define method on the struct
12 func (p *person) Speak() {
13 fmt.Printf("Hello, I'm %v %v and I am %v years oldn", p.FirstName, p.LastName, p.Age)
14 }
15
16 func main() {
17 john := person{"John", "Doe", 25}
18
19 //invoke method on the struct
20 john.Speak()
21 } Run
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 22/54
Is golang an O/O language then?
From o cial GO FAQ: Yes and no :)
Although Go has types and methods and allows an object-oriented style of programming,
there is no type hierarchy.
Moreover, methods in Go are more general than in C++ or Java: they can be de ned for any
sort of data, even built-in types such as plain, “unboxed” integers.
They are not restricted to structs (classes).
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 23/54
Type system summary
No generic types, no type hierarchy
built-in (simple) types: bool, int, int64, string...
(BTW: string is NOT pointer type, zero value for string is ""!)
interfaces
structs
functions
pointers
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 24/54
Pointers...
1 package main
2
3 import "fmt"
4
5 func main() {
6 i, j := 42, 2701
7
8 p := &i // point to i
9 fmt.Println(*p) // read i through the pointer
10 *p = 21 // set i through the pointer
11 fmt.Println(i) // see the new value of i
12
13 p = &j // point to j
14 *p = *p / 37 // divide j through the pointer
15 fmt.Println(j) // see the new value of j
16 } Run
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 25/54
Pointers, cont.
There is no pointer arithmetic.
Used for arguments of functions/methods
Since Go uses "pass-by-value", use pointers to avoid copying data
Because strings are NOT pointers, chance of nil pointer errors is reduced compared to
e.g: Java
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 26/54
Any questions so far?
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 27/54
Compilation
Go compiles to a single statically-linked executable binary
Go binary includes run-time library but it's NOT a VM, it runs native code
Go run-time library is handling things like GC and goroutines support.
You can cross-compile to a di erent OS (linux, darwin, windows) and arch (x86, arm)
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 28/54
Tools
Go comes with a complete productivity toolset: go <arg> :
build compile packages and dependencies
clean remove object files
doc show documentation for package or symbol
fmt run gofmt on package sources
generate generate Go files by processing source
get download and install packages and dependencies
install compile and install packages and dependencies
list list packages
run compile and run Go program
test test packages
version print Go version
vet run go tool vet on packages
And this presentation is done with present tool:
go get golang.org/x/tools/cmd/present
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 29/54
Why Go was created ? Leo why ?!
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 30/54
Why Go was created ? Leo why ?!
E cient Compilation
E cient & simple execution
Ease of programming
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 31/54
What does Go give us ?
simplicity
performance
consistency
fast builds
support for concurrent programming
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 32/54
Who uses Go?
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 33/54
Who uses Go?
Docker
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 34/54
Who uses Go?
Twitter
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 35/54
Who uses Go?
Uber
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 36/54
Who uses Go?
SpaceX
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 37/54
Who uses Go?
Many others like :
CoreOS
Adobe
Dropbox
In uxDB
nd out more at :
github.com/golang/go/wiki/GoUsers(https://github.com/golang/go/wiki/GoUsers)
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 38/54
Go examples - part 2
Defer
Concurrency
Unit tests
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 39/54
Defer
What is the result?
package main
import "fmt"
func main() {
defer fmt.Println("Hello")
fmt.Println("World")
} Run
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 40/54
Defer part 2
package main
import "os"
import "fmt"
func main() {
f, err := os.Open("abc.txt")
if err != nil {
fmt.Println("Got error", err)
return
}
defer f.Close()
f.WriteString("data")
} Run
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 41/54
Finally in Python
try:
f = open(filePath, 'w')
except IOError:
msg = ("Unable to create file on disk.")
f.close()
return
finally:
if f != None:
f.write("Hello World!")
f.close()
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 42/54
Goroutines
A goroutine is a lightweight thread managed by the Go runtime.
package main
import (
"time"
"fmt"
)
func sayHello(name string) {
for i := 0; i < 3; i++ {
time.Sleep(50 * time.Millisecond)
fmt.Println("hello ", name)
}
}
func main() {
go sayHello("Piotr")
sayHello("Tomasz")
} Run
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 43/54
Concurrecny in Go vs Java
go func() {
fmt.Println("Hello")
}()
new Thread(() -> {
System.out.println("Hello from Thread");
}).start();
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 44/54
Million goroutines
package main
import "sync"
import "time"
import "fmt"
import "math/rand"
func main() {
wg := sync.WaitGroup{}
bef := time.Now()
const len = 1000000 // million
wg.Add(len)
var out [len]int32
for i := 0; i < len; i++ {
go func(idx int) {
out[idx] = rand.Int31n(1000)
wg.Done()
}(i)
}
wg.Wait()
fmt.Println(time.Now().Sub(bef))
} Run
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 45/54
Channels
Do not communicate by sharing memory; instead, share memory by communicating.
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 46/54
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 47/54
Channels - example
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan int)
// consumer
go func() {
for {
v := <-ch
fmt.Println(v)
time.Sleep(500 * time.Millisecond)
}
}()
for i := 0; i < 10; i ++ {
ch <- i
}
} Run
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 48/54
Channels - timeout
package main
import (
"time"
"fmt"
)
func main() {
ch := make(chan int)
go func() {
time.Sleep(100 * time.Millisecond)
ch <- 1
}()
select {
case v := <- ch:
fmt.Println("Received from channel", v)
case <- time.After(150 * time.Millisecond):
fmt.Println("Timeout")
}
} Run
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 49/54
Testing
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func sum(a, b int) int {
return a + b
}
func TestSum(t *testing.T) {
if sum(2, 3) != 5 {
t.Errorf("failed")
}
}
func TestSumWithAssertions(t *testing.T) {
assert.Equal(t, 5, sum(2, 3))
}
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 50/54
Table testing
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func sum(a, b int) int {
return a + b
}
func TestSumTables(t *testing.T) {
for _, tc := range []struct {
A int
B int
Exp int
} {
{A: 1, B: 2, Exp: 3}, {A: 10, B: 2, Exp: 12}, {A: -10, B: 2, Exp: -8},
} {
t.Run("", func(t *testing.T) {
assert.Equal(t, tc.Exp, sum(tc.A, tc.B))
})
}
}
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 51/54
Summary
Key concepts
What does Go give us?
Why should we learn Go
Welcome to next Go presentations
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 52/54
Survey
www.surveymonkey.com/r/H7MNM7H(https://www.surveymonkey.com/r/H7MNM7H)
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 53/54
Thank you
19/10/2017 Go - Introduction
http://127.0.0.1:3999/presentation.slide#1 54/54

More Related Content

PDF
Implementing Virtual Machines in Ruby & C
PPT
PDF
Netbeans keyboard shortcut
PPTX
Let's talks about string operations in C++17
PDF
Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko
PDF
Introduction to go language programming
PDF
Golang 101
PDF
Inroduction to golang
Implementing Virtual Machines in Ruby & C
Netbeans keyboard shortcut
Let's talks about string operations in C++17
Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko
Introduction to go language programming
Golang 101
Inroduction to golang

Similar to Go introduction (20)

PPTX
Golang basics for Java developers - Part 1
PDF
To GO or not to GO
PPTX
Go. Why it goes
PDF
Go 1.10 Release Party - PDX Go
PDF
Coding in GO - GDG SL - NSBM
PPTX
The GO Language : From Beginners to Gophers
PDF
Golang and Eco-System Introduction / Overview
PPTX
Golang iran - tutorial go programming language - Preliminary
PDF
Go Programming by Example_ Nho Vĩnh Share.pdf
PDF
Let's golang
PPTX
Go programming introduction
PDF
Golang
PDF
Geeks Anonymes - Le langage Go
PPTX
Go Programming Language (Golang)
PDF
Mender.io | Develop embedded applications faster | Comparing C and Golang
PDF
Golang workshop
PPTX
Linux
PDF
Go serving: Building server app with go
PPTX
Lab2_AdvancedGoConceptswithgo_foundationofgolang_.pptx
PDF
Introduction to Programming in Go
Golang basics for Java developers - Part 1
To GO or not to GO
Go. Why it goes
Go 1.10 Release Party - PDX Go
Coding in GO - GDG SL - NSBM
The GO Language : From Beginners to Gophers
Golang and Eco-System Introduction / Overview
Golang iran - tutorial go programming language - Preliminary
Go Programming by Example_ Nho Vĩnh Share.pdf
Let's golang
Go programming introduction
Golang
Geeks Anonymes - Le langage Go
Go Programming Language (Golang)
Mender.io | Develop embedded applications faster | Comparing C and Golang
Golang workshop
Linux
Go serving: Building server app with go
Lab2_AdvancedGoConceptswithgo_foundationofgolang_.pptx
Introduction to Programming in Go
Ad

Recently uploaded (20)

PDF
top salesforce developer skills in 2025.pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPT
Introduction Database Management System for Course Database
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
System and Network Administration Chapter 2
PPTX
ai tools demonstartion for schools and inter college
PPTX
Introduction to Artificial Intelligence
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Nekopoi APK 2025 free lastest update
top salesforce developer skills in 2025.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Introduction Database Management System for Course Database
Design an Analysis of Algorithms I-SECS-1021-03
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Computer Software and OS of computer science of grade 11.pptx
Odoo Companies in India – Driving Business Transformation.pdf
System and Network Administration Chapter 2
ai tools demonstartion for schools and inter college
Introduction to Artificial Intelligence
wealthsignaloriginal-com-DS-text-... (1).pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PTS Company Brochure 2025 (1).pdf.......
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Softaken Excel to vCard Converter Software.pdf
Nekopoi APK 2025 free lastest update
Ad

Go introduction

  • 1. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 1/54 Go - Introduction October 2017
  • 2. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 2/54 Go package main import "fmt" func main() { fmt.Println("Go - Introductionnn") for _, speaker := range []string{"Tomasz Smelcerz", "Piotr Miśkiewicz", "Piotr Mścichowski"} { fmt.Println(speaker) } } Run
  • 3. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 3/54 Speakers Piotr Miśkiewicz piotr.miskiewicz (at) sap.com Piotr Mścichowski piotr.mscichowski (at) hybris.com Tomasz Smelcerz tomasz.smelcerz (at) hybris.com
  • 4. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 4/54 Brought to you by Hack Your Career www.facebook.com/Hack.your.Career(https://www.facebook.com/Hack.your.Career)
  • 5. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 5/54 Agenda Go key concepts Where to use Go Code examples - part 1 Why Go was created Who's using Go Code examples - part 2 Summary
  • 6. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 6/54 What's in it for me ?
  • 7. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 7/54 What's in it for me ? insights.stackover ow.com/survey/2017#top-paying-technologies(https://insights.stackover ow.com/survey/2017#top- paying-technologies)
  • 8. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 8/54
  • 9. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 9/54 What's in it for me ? insights.stackover ow.com/survey/2017#most-loved-dreaded-and-wanted (https://insights.stackover ow.com/survey/2017#most-loved-dreaded-and-wanted)
  • 10. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 10/54 Golang key concepts Easy syntax Statically typed Duck typing Concurrency Garbage Collector on board
  • 11. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 11/54 What for ? For applications where high performance matters OS applications Network applications Backend applications
  • 12. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 12/54 Go examples - part 1 We'll just give you an overview of the most important features For more detailed info, see: - tour.golang.org/welcome/1(https://tour.golang.org/welcome/1) - gobyexample.com(https://gobyexample.com)
  • 13. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 13/54 Functions 1 package main 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 func printUpper(message string) { 9 fmt.Println(strings.ToUpper(message)) 10 } 11 12 func main() { 13 printUpper("Golang") 14 } Run
  • 14. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 14/54 First-Class Functions 1 package main 2 3 import "fmt" 4 5 func intSeq() func() int { 6 i := 0 7 return func() int { 8 i += 1 9 return i 10 } 11 } 12 13 func main() { 14 15 nextInt := intSeq() 16 17 fmt.Println(nextInt()) 18 fmt.Println(nextInt()) 19 fmt.Println(nextInt()) 20 21 newInts := intSeq() 22 fmt.Println(newInts()) 23 } Run
  • 15. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 15/54 Multiple return values 1 package main 2 3 import "fmt" 4 5 func swap(a, b int) (int, int) { 6 return b, a 7 } 8 9 func main() { 10 x, y := swap(1, 2) 11 12 fmt.Println(x) 13 fmt.Println(y) 14 } Run
  • 16. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 16/54 Error Handling 1 package main 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 ) 8 9 func strToInt(s string) (int, error) { 10 s = strings.Trim(s, " ") 11 val, err := strconv.Atoi(s) 12 return val, err 13 } 14 15 func main() { 16 val, err := strToInt(" 45 ") 17 if err != nil { 18 fmt.Printf("Error: %s", err.Error()) 19 return 20 } 21 fmt.Println(val) 22 } Run
  • 17. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 17/54 Error Handling The error type is an interface type. An error variable represents any value that can describe itself as a string. Here is the interface's declaration: type error interface { Error() string }
  • 18. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 18/54 Interfaces Interfaces are named collections of method signatures. We Use them to de ne contracts (like in Java) We can implement an interface on objects of other types (usually structs)
  • 19. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 19/54 Duck Typing in Go "If it walks like a duck, and it quacks like a duck, then it is a duck" No need to declare that a type implements an interface All you need is to implement all the methods of the interface (on some type) In go structs, simple types and even function types can implement interfaces
  • 20. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 20/54 Interface implementation example 8 //start 9 type shape interface { 10 Area() float64 11 } 12 13 func describe(s shape) { 14 fmt.Println("My area is ", s.Area()) 15 } 16 17 type circle struct { 18 radius float64 19 } 20 21 //This is how we define method on a struct 22 func (c circle) Area() float64 { 23 return math.Pi * c.radius * c.radius 24 } 25 26 func main() { 27 describe(circle{radius: 10}) 28 } 29 30 //end Run
  • 21. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 21/54 More on structs 1 package main 2 3 import "fmt" 4 5 type person struct { 6 FirstName string 7 LastName string 8 Age int 9 } 10 11 //Define method on the struct 12 func (p *person) Speak() { 13 fmt.Printf("Hello, I'm %v %v and I am %v years oldn", p.FirstName, p.LastName, p.Age) 14 } 15 16 func main() { 17 john := person{"John", "Doe", 25} 18 19 //invoke method on the struct 20 john.Speak() 21 } Run
  • 22. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 22/54 Is golang an O/O language then? From o cial GO FAQ: Yes and no :) Although Go has types and methods and allows an object-oriented style of programming, there is no type hierarchy. Moreover, methods in Go are more general than in C++ or Java: they can be de ned for any sort of data, even built-in types such as plain, “unboxed” integers. They are not restricted to structs (classes).
  • 23. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 23/54 Type system summary No generic types, no type hierarchy built-in (simple) types: bool, int, int64, string... (BTW: string is NOT pointer type, zero value for string is ""!) interfaces structs functions pointers
  • 24. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 24/54 Pointers... 1 package main 2 3 import "fmt" 4 5 func main() { 6 i, j := 42, 2701 7 8 p := &i // point to i 9 fmt.Println(*p) // read i through the pointer 10 *p = 21 // set i through the pointer 11 fmt.Println(i) // see the new value of i 12 13 p = &j // point to j 14 *p = *p / 37 // divide j through the pointer 15 fmt.Println(j) // see the new value of j 16 } Run
  • 25. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 25/54 Pointers, cont. There is no pointer arithmetic. Used for arguments of functions/methods Since Go uses "pass-by-value", use pointers to avoid copying data Because strings are NOT pointers, chance of nil pointer errors is reduced compared to e.g: Java
  • 26. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 26/54 Any questions so far?
  • 27. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 27/54 Compilation Go compiles to a single statically-linked executable binary Go binary includes run-time library but it's NOT a VM, it runs native code Go run-time library is handling things like GC and goroutines support. You can cross-compile to a di erent OS (linux, darwin, windows) and arch (x86, arm)
  • 28. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 28/54 Tools Go comes with a complete productivity toolset: go <arg> : build compile packages and dependencies clean remove object files doc show documentation for package or symbol fmt run gofmt on package sources generate generate Go files by processing source get download and install packages and dependencies install compile and install packages and dependencies list list packages run compile and run Go program test test packages version print Go version vet run go tool vet on packages And this presentation is done with present tool: go get golang.org/x/tools/cmd/present
  • 29. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 29/54 Why Go was created ? Leo why ?!
  • 30. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 30/54 Why Go was created ? Leo why ?! E cient Compilation E cient & simple execution Ease of programming
  • 31. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 31/54 What does Go give us ? simplicity performance consistency fast builds support for concurrent programming
  • 32. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 32/54 Who uses Go?
  • 33. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 33/54 Who uses Go? Docker
  • 34. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 34/54 Who uses Go? Twitter
  • 35. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 35/54 Who uses Go? Uber
  • 36. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 36/54 Who uses Go? SpaceX
  • 37. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 37/54 Who uses Go? Many others like : CoreOS Adobe Dropbox In uxDB nd out more at : github.com/golang/go/wiki/GoUsers(https://github.com/golang/go/wiki/GoUsers)
  • 38. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 38/54 Go examples - part 2 Defer Concurrency Unit tests
  • 39. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 39/54 Defer What is the result? package main import "fmt" func main() { defer fmt.Println("Hello") fmt.Println("World") } Run
  • 40. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 40/54 Defer part 2 package main import "os" import "fmt" func main() { f, err := os.Open("abc.txt") if err != nil { fmt.Println("Got error", err) return } defer f.Close() f.WriteString("data") } Run
  • 41. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 41/54 Finally in Python try: f = open(filePath, 'w') except IOError: msg = ("Unable to create file on disk.") f.close() return finally: if f != None: f.write("Hello World!") f.close()
  • 42. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 42/54 Goroutines A goroutine is a lightweight thread managed by the Go runtime. package main import ( "time" "fmt" ) func sayHello(name string) { for i := 0; i < 3; i++ { time.Sleep(50 * time.Millisecond) fmt.Println("hello ", name) } } func main() { go sayHello("Piotr") sayHello("Tomasz") } Run
  • 43. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 43/54 Concurrecny in Go vs Java go func() { fmt.Println("Hello") }() new Thread(() -> { System.out.println("Hello from Thread"); }).start();
  • 44. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 44/54 Million goroutines package main import "sync" import "time" import "fmt" import "math/rand" func main() { wg := sync.WaitGroup{} bef := time.Now() const len = 1000000 // million wg.Add(len) var out [len]int32 for i := 0; i < len; i++ { go func(idx int) { out[idx] = rand.Int31n(1000) wg.Done() }(i) } wg.Wait() fmt.Println(time.Now().Sub(bef)) } Run
  • 45. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 45/54 Channels Do not communicate by sharing memory; instead, share memory by communicating.
  • 46. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 46/54
  • 47. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 47/54 Channels - example package main import ( "fmt" "time" ) func main() { ch := make(chan int) // consumer go func() { for { v := <-ch fmt.Println(v) time.Sleep(500 * time.Millisecond) } }() for i := 0; i < 10; i ++ { ch <- i } } Run
  • 48. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 48/54 Channels - timeout package main import ( "time" "fmt" ) func main() { ch := make(chan int) go func() { time.Sleep(100 * time.Millisecond) ch <- 1 }() select { case v := <- ch: fmt.Println("Received from channel", v) case <- time.After(150 * time.Millisecond): fmt.Println("Timeout") } } Run
  • 49. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 49/54 Testing package main import ( "testing" "github.com/stretchr/testify/assert" ) func sum(a, b int) int { return a + b } func TestSum(t *testing.T) { if sum(2, 3) != 5 { t.Errorf("failed") } } func TestSumWithAssertions(t *testing.T) { assert.Equal(t, 5, sum(2, 3)) }
  • 50. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 50/54 Table testing package main import ( "testing" "github.com/stretchr/testify/assert" ) func sum(a, b int) int { return a + b } func TestSumTables(t *testing.T) { for _, tc := range []struct { A int B int Exp int } { {A: 1, B: 2, Exp: 3}, {A: 10, B: 2, Exp: 12}, {A: -10, B: 2, Exp: -8}, } { t.Run("", func(t *testing.T) { assert.Equal(t, tc.Exp, sum(tc.A, tc.B)) }) } }
  • 51. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 51/54 Summary Key concepts What does Go give us? Why should we learn Go Welcome to next Go presentations
  • 52. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 52/54 Survey www.surveymonkey.com/r/H7MNM7H(https://www.surveymonkey.com/r/H7MNM7H)
  • 53. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 53/54 Thank you
  • 54. 19/10/2017 Go - Introduction http://127.0.0.1:3999/presentation.slide#1 54/54