In this talk, I spoke about parallelism and concurrency and how you can achieve the same behavioral pattern you can achieve with Node.js using await/async statements, using goroutines and channels with Golang.
4. Version 1.0
released
March, 2012
Go first appeared
in Google,
2007
November, 2009
Google announce Golang
to the worldwide
community
June, 2017
I released a
go-perceptron
on Github
Golang Developer Group
begins in Turin
January, 2020
2020
The community
grows!
19. package main
import "fmt"
func main() {
var a = "initial"
fmt.Println(a) # initial
var b, c int = 1, 2
fmt.Println(a, b) # 1, 2
var d = true
fmt.Println(d) # true
var e int
fmt.Println(e) # 0
f := "apple"
fmt.Println(f) # apple
}
22. Concurrency is the composition of independently
executing processes, while parallelism is the
simultaneous execution of (possibly related)
computations.
23. Introducing
goroutines
package main
import (
"fmt"
"time"
)
func f(from string) {
for i := 0; i < 3; i++ {
fmt.Println(from, ":", i)
}
}
func main() {
f("direct")
go f("goroutine")
go func(msg string) {
fmt.Println(msg)
}("going")
time.Sleep(time.Second)
fmt.Println("done")
}
24. Introducing
goroutines
package main
import (
"fmt"
"time"
)
func f(from string) {
for i := 0; i < 3; i++ {
fmt.Println(from, ":", i)
}
}
func main() {
f("direct")
go f("goroutine")
go func(msg string) {
fmt.Println(msg)
}("going")
time.Sleep(time.Second)
fmt.Println("done")
}
# standard function call
25. Introducing
goroutines
package main
import (
"fmt"
"time"
)
func f(from string) {
for i := 0; i < 3; i++ {
fmt.Println(from, ":", i)
}
}
func main() {
f("direct")
go f("goroutine")
go func(msg string) {
fmt.Println(msg)
}("going")
time.Sleep(time.Second)
fmt.Println("done")
}
# standard function call
# to invoke this function
in a goroutine, use go f(s).
This new goroutine will
execute concurrently with
the calling one.
26. When we run this program, we see
the output of the blocking call
first, then the interleaved output of
the two goroutines.
This interleaving reflects the
goroutines being run concurrently
by the Go runtime.
30. package main
import "fmt"
func main() {
messages := make(chan string)
go func() { messages <- "ping" }()
msg := <-messages
fmt.Println(msg)
}
# channels are typed by the
values they convey
Introducing
channels
31. package main
import "fmt"
func main() {
messages := make(chan string)
go func() { messages <- "ping" }()
msg := <-messages
fmt.Println(msg)
}
# channels are typed by the
values they convey
# when we run the program
the "ping" message is
successfully passed from one
goroutine to another via our
channel.
Introducing
channels
32. By default sends and receives block until
both the sender and receiver are ready.
This property allowed us to wait at the end
of our program for the "ping" message
without having to use any other
synchronization.
33. Philosophically, the idea behind Go is:
Don't communicate by sharing
memory; share memory by
communicating.
36. Promise
A promise is a special JavaScript
object that let links production and
consuming code
37. async
Before a function means “this function
always returns a Promise”
Promise
A promise is a special JavaScript
object that let links production and
consuming code
38. async
Before a function means “this function
always returns a Promise”
await
Works only inside async function and makes
JavaScript wait until that Promise settles
and returns its result
Promise
A promise is a special JavaScript
object that let links production and
consuming code
39. const sleep = require('util').promisify(setTimeout)
async function myAsyncFunction() {
await sleep(2000)
return 2
};
(async function() {
const result = await myAsyncFunction();
// outputs `2` after two seconds
console.log(result);
})();
40. // ... package main and imports
func myAsyncFunction() <-chan int32 {
r := make(chan int32)
go func() {
defer close(r)
// work to be completed
time.Sleep(time.Second * 2)
r <- 2
}()
return r
}
func main() {
r := <-myAsyncFunction()
// outputs `2` after two seconds
fmt.Println(r)
}
42. const myAsyncFunction = (s) => {
return new Promise((resolve) => {
setTimeout(() => resolve(s), 2000);
})
};
(async function() {
const result = await Promise.all([
myAsyncFunction(2),
myAsyncFunction(3)
]);
// outputs `2, 3` after two seconds
console.log(result);
})();
43. // ... package main and imports
func myAsyncFunction() <-chan int32 {
r := make(chan int32)
go func() {
defer close(r)
// work to be completed
time.Sleep(time.Second * 2)
r <- 2
}()
return r
}
func main() {
firstChannel, secondChannel :=
myAsyncFunction(2), myAsyncFunction(3)
first, second := <-firstChannel, <-secondChannel
// outputs `2, 3` after two seconds
fmt.Println(first, second)
}
44. The cool thing about channels is that
you can use Go's select statement
to implement concurrency patterns
and wait on multiple channel
operations.
46. const myAsyncFunction = (s) => {
return new Promise((resolve) => {
setTimeout(() => resolve(s), 2000);
})
};
(async function() {
const result = await Promise.race([
myAsyncFunction(2),
myAsyncFunction(3)
]);
// outputs `2` or `3` after two seconds
console.log(result);
})();
47. // ... package main and imports
func myAsyncFunction() <-chan int32 {
r := make(chan int32)
go func() {
defer close(r)
// work to be completed
time.Sleep(time.Second * 2)
r <- 2
}()
return r
}
func main() {
var r int32
select {
case r = <-myAsyncFunction(2)
case r = <-myAsyncFunction(3)
}
// outputs `2` or `3` after two seconds
fmt.Println(r)
}