Your browser doesn't support the features required by impress.js,
so you are presented with a simplified
version of this presentation.
For the best experience please use the latest Chrome,
Safari or Firefox browser.
Going Async in Go
Johannes Pelto-Piri & Oskar Wickström
What is Go?
Go is an open source programming environment that makes it
easy to build simple, reliable, and efficient software.
Overview
- Open source, backed by Google
- Compiled
- Statically typed
- Garbage collected
- Concurrent
So what does it look like?
Hello World in Go
Packages
-
Functions, types, variables and constants in packages are visible
from other packages if the first character is uppercase. E.g.
MyFunction
is visible, myState
is not.
-
A main function must reside in the
main
package.
-
A package name is the last name in the import path. When
importing
"net/http"
you are importing the http
package.
-
The package name does not need to be unique in the linked executable,
only the full import path.
Variables
References and Values
-
When passing an object as a value, the struct is
copied
to
the stack.
-
When passing an object as a reference, you pass a pointer to the object.
-
References can be dereferenced (
*reference
) and values can be
referenced (&value
).
Slices
-
Slices wraps the more low level arrays in Go, and are the most
commonly used
sequence type.
- They are variable length and can have a fixed capacity.
Slices and Slicing
Maps
- Go provides a built-in map type that implements a hash table.
- Keys must be
Comparable
while values can be anything.
- Maps are not safe when used concurrently.
- No iteration order.
Working with Maps
Multiple Return Values
Defer
-
defer
schedules a function to be called before
returning from the function calling defer
.
-
The arguments of the deferred function are evaluated when the
function is called, not at the call to
defer
.
-
The order of deferred function execution is FILO.
Closing a file with defer
Interfaces
- They are satisfied implicitly.
- Statically checked to avoid runtime errors.
- In short, statically checked duck typing.
Interfaces Example
Concurrency in Go
-
Goroutines are small lightweight processes that are managed by
Go's runtime.
- Channels are first class citizen that connects Goroutines.
-
A channel may be buffered or unbuffered, an unbuffered
channel will always block until there is a receiver.
- Channels are by default unbuffered.
Ping Pong
Goroutines are Cheap
- ~4KB initial memory overhead.
- They are multiplexed across OS threads.
- Goroutines do not block on IO.
Non-blocking IO in Goroutines
What about something more advanced?
Lots of routines and channels
Buffered Channels
Select over Channels
Timing Out
Channel Directions
- Channels can be typed to have a direction.
chan<- string
can only be sent to.
<-chan string
can only be received from.
Closing Channels
Iterating Channel Messages
Timers & Tickers using Channels
Other Channel Operations
-
len(c)
returns the number of elements in a channel. In
a non-buffered channel this is always 0.
-
cap(c)
returns the element capacity in a channel. In
a non-buffered channel this is always 0.
GOMAXPROCS
-
Sets the maximum number of OS threads used for Goroutines multiplexing.
- Can be set programatically with
runtime.GOMAXPROCS(0)
.
- Will be removed when the Go scheduler improves.
Demo
Questions?
Use a spacebar or arrow keys to navigate