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

Similar to Go introduction

Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Robert Stern
 
To GO or not to GO
To GO or not to GOTo GO or not to GO
To GO or not to GOsuperstas88
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoRodolfo Carvalho
 
Coding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBMCoding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBMRaveen Perera
 
The GO Language : From Beginners to Gophers
The GO Language : From Beginners to GophersThe GO Language : From Beginners to Gophers
The GO Language : From Beginners to GophersAlessandro Sanino
 
Golang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewGolang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewMarkus Schneider
 
Golang iran - tutorial go programming language - Preliminary
Golang iran - tutorial  go programming language - PreliminaryGolang iran - tutorial  go programming language - Preliminary
Golang iran - tutorial go programming language - Preliminarygo-lang
 
Go Programming by Example_ Nho Vĩnh Share.pdf
Go Programming by Example_ Nho Vĩnh Share.pdfGo Programming by Example_ Nho Vĩnh Share.pdf
Go Programming by Example_ Nho Vĩnh Share.pdfNho Vĩnh
 
Go programming introduction
Go programming introductionGo programming introduction
Go programming introductionGinto Joseph
 
Geeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)Ishin Vin
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io
 
Go serving: Building server app with go
Go serving: Building server app with goGo serving: Building server app with go
Go serving: Building server app with goHean Hong Leong
 
Introduction to Programming in Go
Introduction to Programming in GoIntroduction to Programming in Go
Introduction to Programming in GoAmr Hassan
 
Developing R Graphical User Interfaces
Developing R Graphical User InterfacesDeveloping R Graphical User Interfaces
Developing R Graphical User InterfacesSetia Pramana
 

Similar to Go introduction (20)

Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1
 
To GO or not to GO
To GO or not to GOTo GO or not to GO
To GO or not to GO
 
Go. Why it goes
Go. Why it goesGo. Why it goes
Go. Why it goes
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX Go
 
Coding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBMCoding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBM
 
The GO Language : From Beginners to Gophers
The GO Language : From Beginners to GophersThe GO Language : From Beginners to Gophers
The GO Language : From Beginners to Gophers
 
Golang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewGolang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / Overview
 
Golang iran - tutorial go programming language - Preliminary
Golang iran - tutorial  go programming language - PreliminaryGolang iran - tutorial  go programming language - Preliminary
Golang iran - tutorial go programming language - Preliminary
 
Go Programming by Example_ Nho Vĩnh Share.pdf
Go Programming by Example_ Nho Vĩnh Share.pdfGo Programming by Example_ Nho Vĩnh Share.pdf
Go Programming by Example_ Nho Vĩnh Share.pdf
 
Let's golang
Let's golangLet's golang
Let's golang
 
Go programming introduction
Go programming introductionGo programming introduction
Go programming introduction
 
Golang
GolangGolang
Golang
 
Geeks Anonymes - Le langage Go
Geeks Anonymes - Le langage GoGeeks Anonymes - Le langage Go
Geeks Anonymes - Le langage Go
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and Golang
 
Golang workshop
Golang workshopGolang workshop
Golang workshop
 
Linux
LinuxLinux
Linux
 
Go serving: Building server app with go
Go serving: Building server app with goGo serving: Building server app with go
Go serving: Building server app with go
 
Introduction to Programming in Go
Introduction to Programming in GoIntroduction to Programming in Go
Introduction to Programming in Go
 
Developing R Graphical User Interfaces
Developing R Graphical User InterfacesDeveloping R Graphical User Interfaces
Developing R Graphical User Interfaces
 

Recently uploaded

Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILNatan Silnitsky
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageGlobus
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfkalichargn70th171
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandIES VE
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAlluxio, Inc.
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxwottaspaceseo
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareinfo611746
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfAMB-Review
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesKrzysztofKkol1
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessWSO2
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfkalichargn70th171
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfOrtus Solutions, Corp
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobus
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAlluxio, Inc.
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptxGeorgi Kodinov
 

Recently uploaded (20)

Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 

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