SlideShare una empresa de Scribd logo
1 de 97
Descargar para leer sin conexión
Introduction to Go
a tutorial for developers

Hello! While you’re sitting down, please make	

sure your Go environment is set up.	

!

Grab a USB stick that’s floating around and copy	

the gostick directory somewhere local.	

!

Then read the included README please!
Autobiography
•

Mark Smith <mark@qq.is> @zorkian	


•

Site Reliability Engineer at Dropbox	


•

Contributes to Dreamwidth Studios	


•

github.com/xb95
Today’s Plan
•

What is Go? Where does it fit?	


•

Obligatory Hello World	


•

Contrived example program	


•

Anti-unicorns	


•

Wrap-up
Go in a Nutshell
•

Designed for building systems	


•

Statically compiled, garbage collected	


•

Static typing (w/some duck typing)	


•

Built-in maps (dicts) and strings
Go in a Nutshell
•

Multi-core support	


•

Concurrency primitives	


•

Closures, etc. etc.	


•

“Somewhere between C and Python”
The Go Way
•

Go is an opinionated language	


•

gofmt, mixedCaps, capitalization for privacy…	


•

Simple is better than complex	


•

The compiler should help with the heavy lifting
Hello World
Hello World
package main
!

import "fmt"
!

func main() {
fmt.Println("Hello, LCA 2014!")
}
Hello World
package main
!

import "fmt"
!

func main() {
fmt.Println("Hello, LCA 2014!")
}
Hello World
package main
!

import "fmt"
!

func main() {
fmt.Println("Hello, LCA 2014!")
}
Hello World
package main
!

import "fmt"
!

func main() {
fmt.Println("Hello, LCA 2014!")
}
Hello World
package main
!

import "fmt"
!

func main() {
fmt.Println("Hello, LCA 2014!")
}
Building Hello World
# Do this in your gostick/ copy
# You did follow the README? :-)
cd helloworld/
go build
./helloworld
Getting Real
•

Go is particularly suited for network services	


•

The standard library is fairly comprehensive	


•

Let’s build an echo server!
Echo Server
1. Listen on port for TCP connections	

2. Accept connections	

3. Read text from connection	

4. Write it back to connection
Standard Library
•

Go has a decent standard library	


•

The ecosystem is still fairly young, so it has some
holes and some things aren’t well optimized	


•

Well built for network services	


•

http://golang.org/pkg/
Tip: Searching

When you search the Internet for
information, “Go” is a bad keyword;
everybody uses “golang”.
Echo Server
1. Listen on port for TCP connections
2. Accept connections	

3. Read text from connection	

4. Write it back to connection
Listening for Connections
In the “net” package, you’ll find many useful things, including:
func Listen(net, laddr string) (Listener, error)
Listening for Connections
In the “net” package, you’ll find many useful things, including:
func Listen(net, laddr string) (Listener, error)

package main
!

import "net"
!

func main() {
!

}
Listening for Connections
In the “net” package, you’ll find many useful things, including:
func Listen(net, laddr string) (Listener, error)

package main
!

import "net"
!

func main() {
... := net.Listen("tcp", ":9000")
}
Variable Definition
•

Go has two ways of declaring variables, explicit and implicit	


•

Explicit:

var foobar uint64	


•

Implicit (type is inferred automatically):

foobar := thingThatReturnsUint64()	


•

Go strives to save on typing redundant information that
the compiler can figure out
Error Handling
•

Using multiple return values to get errors	


•

Idiomatically, you will write this a lot:



results, err := pkg.DoSomething(1, 2)

if err != nil {

log.Fatalf(“Failed: %s”, err)

}

// carry on and use results	


•

Yes, this gets very verbose… oh well
Echo Server
1. Listen on port for TCP connections	

2. Accept connections
3. Read text from connection	

4. Write it back to connection
Connection Pump
func Accept() (Conn, error)
func main() {
listener, err := net.Listen("tcp", ":9000")
!
!
!
!
!
!
!
!
!
!
!
}
Connection Pump
func Accept() (Conn, error)
func main() {
listener, err := net.Listen("tcp", ":9000")
if err != nil {
panic(err)
}
!
!
!
!
!
!
!
!
}
Connection Pump
func Accept() (Conn, error)
func main() {
listener, err := net.Listen("tcp", ":9000")
if err != nil {
panic(err)
}
!
for {

!
!
!
!
}
}
Connection Pump
func Accept() (Conn, error)
func main() {
listener, err := net.Listen("tcp", ":9000")
if err != nil {
panic(err)
}
!
for {
client, err := listener.Accept()
!
!
!
!
}
}
Connection Pump
func Accept() (Conn, error)
func main() {
listener, err := net.Listen("tcp", ":9000")
if err != nil {
panic(err)
}
!
for {
client, err := listener.Accept()
if err != nil {
continue
}
!
}
}
Connection Pump
func Accept() (Conn, error)
func main() {
listener, err := net.Listen("tcp", ":9000")
if err != nil {
panic(err)
}
!
for {
client, err := listener.Accept()
if err != nil {
continue
}
handleClient(client)
}
}
Echo Server
1. Listen on port for TCP connections	

2. Accept connections	

3. Read text from connection
4. Write it back to connection
Client Handler
func Read(b []byte) (int, error)
func handleClient(client net.Conn) {
for {
// read from our client
// write it back to our client
}
}
Client Handler
func Read(b []byte) (int, error)
func handleClient(client net.Conn) {
for {
// read from our client
// write it back to our client
}
}

Okay, so what’s a []byte?
Primitive Go Types
•

All of the usual suspects (ints, uints, floats)	


•

Built-in string type (Unicode, immutable)	


•

int and uint are architecture-width	


•

byte is just a synonym for uint8
More Types
•

arrays: of a declared, fixed length	


•

slice: a segment (“slice”) of an array	


•

map: key/value storage	


•

pointer: much like C (uses & and *)	


•

(more to come later)
So, []byte…
•

Let’s make an array of bytes:

var ary [4096]byte	


•

What if we don’t know the size? Or don’t care?
Slices solve this problem:

var aryslice []byte

•

This is great, but what is it?
Slices Explained
var a [16]byte is
Slices Explained
var a [16]byte is
a[3] is
Slices Explained
var a [16]byte is
a[3] is
a[6:8] is
Slices Explained
var a [16]byte is
a[3] is
a[6:8] is
var s []byte is nothing to start with.
Slices Explained
var a [16]byte is
a[3] is
a[6:8] is
var s []byte is nothing to start with.
s = a[6:8]; s is
Slices Explained
var a [16]byte is
a[3] is
a[6:8] is
var s []byte is nothing to start with.
s = a[6:8]; s is
s[0] is the same as a[6], etc!

A slice is simply a window into a backing array.
Slices pt. 2
•

Slices are internally a tuple of (array, start, length)	


•

A slice can be moved around (a sliding window)
and resized (limited to the backing array size)	


•

Slices are reference types; great for passing to
functions
Echo Server
1. Listen on port for TCP connections	

2. Accept connections	

3. Read text from connection
4. Write it back to connection
Client Handler
func Read(b []byte) (int, error)

func handleClient(client net.Conn) {
for {
!
!
!
!
!
!
}
}
Client Handler
func Read(b []byte) (int, error)

func handleClient(client net.Conn) {
for {
buf := make([]byte, 4096)

!
!
!
!
}
}
Client Handler
func Read(b []byte) (int, error)

func handleClient(client net.Conn) {
for {
buf := make([]byte, 4096)
numbytes, err := client.Read(buf)
!
!
!
!
}
}
Client Handler
func Read(b []byte) (int, error)

func handleClient(client net.Conn) {
for {
buf := make([]byte, 4096)
numbytes, err := client.Read(buf)
if numbytes == 0 || err != nil {
return
}
!
}
}
Client Handler
func Read(b []byte) (int, error)

func handleClient(client net.Conn) {
for {
buf := make([]byte, 4096)
numbytes, err := client.Read(buf)
if numbytes == 0 || err != nil {
return
}
client.Write(buf)
}
}
Build & Test
# Do this in your gostick/ copy
cd part1/
go build
./part1 &
telnet localhost 9000
# say something and hit enter
More Power
•

The echo server is great, but can only serve one
user at a time!	


•

Concurrent network programming… should we
fork for each child? use a threading library? maybe
we can implement it using non-blocking I/O…	


•

Stop, stop, stop!
Concurrent, Go Style
Remember our main accept loop? Let’s tweak it from this…
func main() {
listener, err := net.Listen("tcp", ":9000")
if err != nil {
panic(err)
}
!
for {
client, err := listener.Accept()
if err != nil {
continue
}
handleClient(client)
}
}
Concurrent, Go Style
…to this!
func main() {
listener, err := net.Listen("tcp", ":9000")
if err != nil {
panic(err)
}
!
for {
client, err := listener.Accept()
if err != nil {
continue
}
go handleClient(client)
}
}
Goroutines
•

A goroutine is a function executing concurrently
with other goroutines	


•

Go multiplexes M goroutines onto N processes,
and scales to 100,000+ goroutines (millions
possible, use case dependent)	


•

N is by default only 1, you can tune it
Goroutines pt. 2
•

Everything in Go is designed to be blocking, in
essence, and the idea is to use goroutines	


•

This makes reasoning about software much easier	


•

Go also provides deadlock detection and
backtraces all living goroutines
Echo v2.0, “Chat”
•

Let’s make a chat server out of our echo server	


•

Design goal: any text from one client is echoed
immediately to all connected clients	


•

Well, clearly, our goroutines have to communicate
Communication
•

The common idiom for communicating between
threads in most languages is shared memory	


•

This kind of work is notoriously racy, error prone,
and hard to implement correctly
Communication in Go
•

“Don’t communicate by sharing memory; share
memory by communicating!”	


•

Enter the concept of channels	


•

A built-in goroutine safe method of passing data
Basic Channel Use
ch := make(chan int)

Create a new channel

ch <- 5

Write to a channel
Read from a channel

i := <-ch

The channel in this example is an unbuffered
channel. Reads block until data is available, writes
block until someone reads. Synchronous.
Buffered Channels
ch := make(chan int, 10)

Create a new channel

ch <- 5

Write to a channel
Read from a channel

i := <-ch

The difference: buffered channels won’t block when
inserting data (unless they’re full).
Chat Server
•

We have to make three main modifications:	

1. Store a list of connected clients	

2. Get input out of the clients	

3. Write each input back to every client	


•

Note: this implementation is a reduced example, so it’s a
little unsafe in one place :-)
Chat Server
•

We have to make three main modifications:	

1. Store a list of connected clients
2. Get input out of the clients	

3. Write each input back to every client	


•

Note: this implementation is a reduced example, so it’s a
little unsafe in one place :-)
Connected Clients
func main() {
...
!
!
!
for {
client, err := listener.Accept()
if err != nil {
continue
}
!
go handleClient(client)
}
}
Connected Clients
func main() {
...
!
var clients []net.Conn
!
for {
client, err := listener.Accept()
if err != nil {
continue
}
clients = append(clients, client)
go handleClient(client)
}
}

Build a slice of clients
and then append
each new client to
the slice.	

!

The append built-in
handles automatically
allocating and
growing the backing
array as necessary.
Chat Server
•

We have to make three main modifications:	

1. Store a list of connected clients	

2. Get input out of the clients
3. Write each input back to every client	


•

Note: this implementation is a reduced example, so it’s a
little unsafe in one place :-)
Getting Input
func main() {
...

!
for {
...
clients = append(clients, client)
go handleClient(client)
}
}
!
func handleClient(client net.Conn) {
for {
...
client.Write(buf)
}
}
Getting Input
func main() {
...
input := make(chan []byte, 10)
!
for {
...
clients = append(clients, client)
go handleClient(client)
}
}
!
func handleClient(client net.Conn) {
for {
...
client.Write(buf)
}
}
Getting Input
func main() {
...
input := make(chan []byte, 10)
!
for {
...
clients = append(clients, client)
go handleClient(client, input)
}
}
!
func handleClient(client net.Conn) {
for {
...
client.Write(buf)
}
}
Getting Input
func main() {
...
input := make(chan []byte, 10)
!
for {
...
clients = append(clients, client)
go handleClient(client, input)
}
}
!
func handleClient(client net.Conn, input chan []byte) {
for {
...
client.Write(buf)
}
}
Getting Input
func main() {
...
input := make(chan []byte, 10)
!
for {
...
clients = append(clients, client)
go handleClient(client, input)
}
}
!
func handleClient(client net.Conn, input chan []byte) {
for {
...
client.Write(buf)
input <- buf
}
}
Getting Input
func main() {
...
input := make(chan []byte, 10)
!
for {
...
clients = append(clients, client)
go handleClient(client, input)
}
}
!
func handleClient(client net.Conn, input chan []byte) {
for {
...
client.Write(buf)
Removed the Write!
input <- buf
}
}
What is Happening?
•

handleClient still is a blocking read loop, but
instead of writing back to the client it writes the
bytes onto a channel	


•

main now has a list of all clients and a channel
that everybody is writing input to	


•

Final piece: somebody to read from the channel!
Chat Server
•

We have to make three main modifications:	

1. Store a list of connected clients	

2. Get input out of the clients	

3. Write each input back to every client

•

Note: this implementation is a reduced example, so it’s a
little unsafe in one place :-)
The Chat Manager
func main() {
...
var clients []net.Conn
input := make(chan []byte, 10)
!
!
!
!
We’ve
!
!
!
!
...
}

seen this all before…
The Chat Manager
func main() {
...
var clients []net.Conn
input := make(chan []byte, 10)
go func() {

!
!
!
!
!
}()
...
}

You can create goroutines
out of closures, too.	

!
The Chat Manager
!
func main() {
...
var clients []net.Conn
input := make(chan []byte, 10)
go func() {
for {
message := <-input

!
!
}
}()
...
}

!
!

This is all blocking!
The Chat Manager
!
func main() {
...
var clients []net.Conn
This
input := make(chan []byte, 10)
go func() {
for {
message := <-input
for _, client := range clients {
!
}
}
}()
...
}

!
!

is all blocking!
range and _
for _, client := range clients {..}

•

The range keyword iterates over maps/slices/
arrays and returns the key (or index) and value	


•

Underscore (_) is the anonymous variable (“blank
identifier”), you can use it to discard results
The Chat Manager
func main() {
...
var clients []net.Conn
input := make(chan []byte, 10)
go func() {
for {
message := <-input
for _, client := range clients {
client.Write(message)
}
}
}()
...
}
Build & Test
# Do this in your gostick/ copy
cd part2/
go build
./part2 &
telnet localhost 9000
# say something and hit enter, now connect
# again in another window and chat! :-)
Echo 3.0, “Frotzer”
•

We’re entering the land of extremely contrived
tutorial examples, but…	


•

Let’s give our chat server some behaviors!	


•

When a user chats, we apply some formatting
rules before sending it out to other users
But First: More Type Talk!
•

Go doesn’t have classes (or objects, really)	


•

However, you have named types, and methods are
attached to named types:










type Uppercaser struct{}
!
func (self Uppercaser) Frotz(input []byte) []byte {
return bytes.ToUpper(input)
}
Um, struct{}?
•

Go supports structs, very much like any other
language that has structs	


•

The empty struct is often used for hanging methods	


•

Instantiating a struct type:

foobar := Lowercaser{}

foobar.Frotz(“HELLO”) // “hello”
Interfaces
•

In Go, an interface specifies a collection of
methods attached to a name	


•

Interfaces are implicit (duck typed)











type Frotzer interface {
Frotz([]byte) []byte
}
!
type Uppercaser struct{}
func (self Uppercaser) Frotz(input []byte) []byte {
return bytes.ToUpper(input)
}
Implementing Frotzing
func chatManager(clients *[]net.Conn, input chan []byte
) {
for {
message := <-input
for _, client := range *clients {
client.Write(message)
}
}
}

This is the input handler loop we built a few minutes ago,
except now we pulled it out of main and made it a
function.
Implementing Frotzing
func chatManager(clients *[]net.Conn, input chan []byte,
frotz Frotzer) {
for {
message := <-input
for _, client := range *clients {
client.Write(frotz.Frotz(message))
}
}
}

Now it takes a third argument: an interface. Any type that
implements Frotzer can be used!
Making Frotzers
These are both named types, and both implement
(implicitly!) the Frotzer interface. Thanks, compiler!
type Uppercaser struct{}
func (self Uppercaser) Frotz(input []byte) []byte {
return bytes.ToUpper(input)
}
!
type Lowercaser struct{}
func (self Lowercaser) Frotz(input []byte) []byte {
return bytes.ToLower(input)
}
A New Main
func main() {
...
var clients []net.Conn
input := make(chan []byte, 10)
go chatManager(&clients, input)
!
!
for {
client, err := listener.Accept()
if err != nil {
continue
}
clients = append(clients, client)
go handleClient(client, input)
}
}
A New Main
func main() {
...
var clients []net.Conn
input := make(chan []byte, 10)
go chatManager(&clients, input, Lowercaser{})
go chatManager(&clients, input, Uppercaser{})
!
for {
client, err := listener.Accept()
if err != nil {
continue
}
clients = append(clients, client)
go handleClient(client, input)
}
}
A New Main pt. 2
•

The chatManager functions are spawned into
separate goroutines	


•

Channels are goroutine safe, so they end up
interleaving reads (not guaranteed!)	


•

chatManager doesn’t know what you’re passing it
as a Frotzer, it just knows it can call Frotz on it
Build & Test
# Do this in your gostick/ copy
cd part3/
go build
./part3 &
telnet localhost 9000
# say something and hit enter, your text
# should alternate UPPER/lower
Review
•

A multi-user chat server demonstrating many core
parts of the Go programming language	


•

Built-in concurrency that is easy to use and trivially
allows relatively powerful constructions	


•

A classless, statically type system that still enables
many OO concepts
Go is Awesome :-)

This slide should have unicorns and bunnies on it
Things That Aren’t Unicorns Yet
•

The GC is generally pretty solid, but if you are
doing many allocations and need performance, you
have to do the usual tricks	


•

Standard library is pretty young and not optimized	


•

Overall ecosystem is small (but growing, hi!)
Also: The Scheduler
•

It’s new and has room for improvement	


•

Heavy use of channels between goroutines is
probably faster in one process	


•

You’ll have to play with GOMAXPROCS and
understand your application
Topics Undiscussed
•

Building your own packages (it’s easy)	


•

Importing from external repositories

import pcap “github.com/akrennmair/gopcap”	


•

Using gofmt (do it!)	


•

switch, select, etc.	


•

Type assertions, reflection, even more etc.
Thank you!

Mark Smith <mark@qq.is> @zorkian

SRE at Dropbox (we’re hiring!)

•

Some recommended reading:

Effective Go golang.org/doc/effective_go.html

FAQ golang.org/doc/faq	


•

Many more talks and presentations:

code.google.com/p/go-wiki/wiki/GoTalks	


•

The Go Playground! play.golang.org

Más contenido relacionado

La actualidad más candente

SDPHP - Percona Toolkit (It's Basically Magic)
SDPHP - Percona Toolkit (It's Basically Magic)SDPHP - Percona Toolkit (It's Basically Magic)
SDPHP - Percona Toolkit (It's Basically Magic)Robert Swisher
 
Release with confidence
Release with confidenceRelease with confidence
Release with confidenceJohn Congdon
 
how to use fiddler (Ver eng)
how to use fiddler (Ver eng)how to use fiddler (Ver eng)
how to use fiddler (Ver eng)용진 조
 
Developing cacheable PHP applications - PHPLimburgBE 2018
Developing cacheable PHP applications - PHPLimburgBE 2018Developing cacheable PHP applications - PHPLimburgBE 2018
Developing cacheable PHP applications - PHPLimburgBE 2018Thijs Feryn
 
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)Ericom Software
 
Developing cacheable PHP applications - Confoo 2018
Developing cacheable PHP applications - Confoo 2018Developing cacheable PHP applications - Confoo 2018
Developing cacheable PHP applications - Confoo 2018Thijs Feryn
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisBeyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisFastly
 
Optimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
Optimising Your Front End Workflow With Symfony, Twig, Bower and GulpOptimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
Optimising Your Front End Workflow With Symfony, Twig, Bower and GulpMatthew Davis
 
Xdebug - Your first, last, and best option for troubleshooting PHP code
Xdebug - Your first, last, and best option for troubleshooting PHP codeXdebug - Your first, last, and best option for troubleshooting PHP code
Xdebug - Your first, last, and best option for troubleshooting PHP codeAdam Englander
 
Using Websockets with Play!
Using Websockets with Play!Using Websockets with Play!
Using Websockets with Play!Andrew Conner
 
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHPNortheast PHP - High Performance PHP
Northeast PHP - High Performance PHPJonathan Klein
 
PyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PyCon US 2012 - Web Server Bottlenecks and Performance TuningPyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PyCon US 2012 - Web Server Bottlenecks and Performance TuningGraham Dumpleton
 
Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.Workhorse Computing
 
WebPagetest - Good, Bad & Ugly
WebPagetest - Good, Bad & UglyWebPagetest - Good, Bad & Ugly
WebPagetest - Good, Bad & UglyAaron Peters
 

La actualidad más candente (20)

Web::Scraper
Web::ScraperWeb::Scraper
Web::Scraper
 
Dancing with websocket
Dancing with websocketDancing with websocket
Dancing with websocket
 
SDPHP - Percona Toolkit (It's Basically Magic)
SDPHP - Percona Toolkit (It's Basically Magic)SDPHP - Percona Toolkit (It's Basically Magic)
SDPHP - Percona Toolkit (It's Basically Magic)
 
Release with confidence
Release with confidenceRelease with confidence
Release with confidence
 
X-Debug in Php Storm
X-Debug in Php StormX-Debug in Php Storm
X-Debug in Php Storm
 
how to use fiddler (Ver eng)
how to use fiddler (Ver eng)how to use fiddler (Ver eng)
how to use fiddler (Ver eng)
 
Developing cacheable PHP applications - PHPLimburgBE 2018
Developing cacheable PHP applications - PHPLimburgBE 2018Developing cacheable PHP applications - PHPLimburgBE 2018
Developing cacheable PHP applications - PHPLimburgBE 2018
 
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
 
Developing cacheable PHP applications - Confoo 2018
Developing cacheable PHP applications - Confoo 2018Developing cacheable PHP applications - Confoo 2018
Developing cacheable PHP applications - Confoo 2018
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisBeyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic Analysis
 
WebSockets with Spring 4
WebSockets with Spring 4WebSockets with Spring 4
WebSockets with Spring 4
 
Angular2 ecosystem
Angular2 ecosystemAngular2 ecosystem
Angular2 ecosystem
 
Web sockets in Java
Web sockets in JavaWeb sockets in Java
Web sockets in Java
 
Optimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
Optimising Your Front End Workflow With Symfony, Twig, Bower and GulpOptimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
Optimising Your Front End Workflow With Symfony, Twig, Bower and Gulp
 
Xdebug - Your first, last, and best option for troubleshooting PHP code
Xdebug - Your first, last, and best option for troubleshooting PHP codeXdebug - Your first, last, and best option for troubleshooting PHP code
Xdebug - Your first, last, and best option for troubleshooting PHP code
 
Using Websockets with Play!
Using Websockets with Play!Using Websockets with Play!
Using Websockets with Play!
 
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHPNortheast PHP - High Performance PHP
Northeast PHP - High Performance PHP
 
PyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PyCon US 2012 - Web Server Bottlenecks and Performance TuningPyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PyCon US 2012 - Web Server Bottlenecks and Performance Tuning
 
Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.Selenium sandwich-3: Being where you aren't.
Selenium sandwich-3: Being where you aren't.
 
WebPagetest - Good, Bad & Ugly
WebPagetest - Good, Bad & UglyWebPagetest - Good, Bad & Ugly
WebPagetest - Good, Bad & Ugly
 

Similar a LCA2014 - Introduction to Go

10 reasons to be excited about go
10 reasons to be excited about go10 reasons to be excited about go
10 reasons to be excited about goDvir Volk
 
How to Leverage Go for Your Networking Needs
How to Leverage Go for Your Networking NeedsHow to Leverage Go for Your Networking Needs
How to Leverage Go for Your Networking NeedsDigitalOcean
 
Highly concurrent yet natural programming
Highly concurrent yet natural programmingHighly concurrent yet natural programming
Highly concurrent yet natural programmingInfinit
 
An Introduction to Go
An Introduction to GoAn Introduction to Go
An Introduction to GoCloudflare
 
Tornado Web Server Internals
Tornado Web Server InternalsTornado Web Server Internals
Tornado Web Server InternalsPraveen Gollakota
 
Node.js for Rubists
Node.js for RubistsNode.js for Rubists
Node.js for RubistsSagiv Ofek
 
Go from a PHP Perspective
Go from a PHP PerspectiveGo from a PHP Perspective
Go from a PHP PerspectiveBarry Jones
 
Introduction to Thrift
Introduction to ThriftIntroduction to Thrift
Introduction to ThriftDvir Volk
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughgabriellekuruvilla
 
Monitoring and Debugging your Live Applications
Monitoring and Debugging your Live ApplicationsMonitoring and Debugging your Live Applications
Monitoring and Debugging your Live ApplicationsRobert Coup
 
Streaming 101: Hello World
Streaming 101:  Hello WorldStreaming 101:  Hello World
Streaming 101: Hello WorldJosh Fischer
 
Hack Like It's 2013 (The Workshop)
Hack Like It's 2013 (The Workshop)Hack Like It's 2013 (The Workshop)
Hack Like It's 2013 (The Workshop)Itzik Kotler
 
Networking and Go: An Engineer's Journey (Strangeloop 2019)
Networking and Go: An Engineer's Journey (Strangeloop 2019)Networking and Go: An Engineer's Journey (Strangeloop 2019)
Networking and Go: An Engineer's Journey (Strangeloop 2019)Sneha Inguva
 
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon YangPractical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon YangLyon Yang
 
C101 – Intro to Programming with C
C101 – Intro to Programming with CC101 – Intro to Programming with C
C101 – Intro to Programming with Cgpsoft_sk
 
Servers with Event Machine - David Troy - RailsConf 2011
Servers with Event Machine - David Troy - RailsConf 2011Servers with Event Machine - David Troy - RailsConf 2011
Servers with Event Machine - David Troy - RailsConf 2011David Troy
 

Similar a LCA2014 - Introduction to Go (20)

10 reasons to be excited about go
10 reasons to be excited about go10 reasons to be excited about go
10 reasons to be excited about go
 
How to Leverage Go for Your Networking Needs
How to Leverage Go for Your Networking NeedsHow to Leverage Go for Your Networking Needs
How to Leverage Go for Your Networking Needs
 
Highly concurrent yet natural programming
Highly concurrent yet natural programmingHighly concurrent yet natural programming
Highly concurrent yet natural programming
 
An Introduction to Go
An Introduction to GoAn Introduction to Go
An Introduction to Go
 
Tornado Web Server Internals
Tornado Web Server InternalsTornado Web Server Internals
Tornado Web Server Internals
 
Node.js for Rubists
Node.js for RubistsNode.js for Rubists
Node.js for Rubists
 
Go from a PHP Perspective
Go from a PHP PerspectiveGo from a PHP Perspective
Go from a PHP Perspective
 
Apache Thrift
Apache ThriftApache Thrift
Apache Thrift
 
Introduction to Thrift
Introduction to ThriftIntroduction to Thrift
Introduction to Thrift
 
Python and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthroughPython and Pytorch tutorial and walkthrough
Python and Pytorch tutorial and walkthrough
 
Monitoring and Debugging your Live Applications
Monitoring and Debugging your Live ApplicationsMonitoring and Debugging your Live Applications
Monitoring and Debugging your Live Applications
 
Streaming 101: Hello World
Streaming 101:  Hello WorldStreaming 101:  Hello World
Streaming 101: Hello World
 
About Clack
About ClackAbout Clack
About Clack
 
Hack Like It's 2013 (The Workshop)
Hack Like It's 2013 (The Workshop)Hack Like It's 2013 (The Workshop)
Hack Like It's 2013 (The Workshop)
 
Python, do you even async?
Python, do you even async?Python, do you even async?
Python, do you even async?
 
Java sockets
Java socketsJava sockets
Java sockets
 
Networking and Go: An Engineer's Journey (Strangeloop 2019)
Networking and Go: An Engineer's Journey (Strangeloop 2019)Networking and Go: An Engineer's Journey (Strangeloop 2019)
Networking and Go: An Engineer's Journey (Strangeloop 2019)
 
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon YangPractical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
 
C101 – Intro to Programming with C
C101 – Intro to Programming with CC101 – Intro to Programming with C
C101 – Intro to Programming with C
 
Servers with Event Machine - David Troy - RailsConf 2011
Servers with Event Machine - David Troy - RailsConf 2011Servers with Event Machine - David Troy - RailsConf 2011
Servers with Event Machine - David Troy - RailsConf 2011
 

Más de dreamwidth

From the Inside Out: How Self-Talk Affects Your Community
From the Inside Out: How Self-Talk Affects Your CommunityFrom the Inside Out: How Self-Talk Affects Your Community
From the Inside Out: How Self-Talk Affects Your Communitydreamwidth
 
Chenoweth os bridge 2015 pp
Chenoweth os bridge 2015 ppChenoweth os bridge 2015 pp
Chenoweth os bridge 2015 ppdreamwidth
 
How We Learned To Stop Worrying And Love (or at least live with) GitHub
How We Learned To Stop Worrying And Love (or at least live with) GitHubHow We Learned To Stop Worrying And Love (or at least live with) GitHub
How We Learned To Stop Worrying And Love (or at least live with) GitHubdreamwidth
 
When your code is nearly old enough to vote
When your code is nearly old enough to voteWhen your code is nearly old enough to vote
When your code is nearly old enough to votedreamwidth
 
Hacking In-Group Bias for Fun and Profit
Hacking In-Group Bias for Fun and ProfitHacking In-Group Bias for Fun and Profit
Hacking In-Group Bias for Fun and Profitdreamwidth
 
Slytherin 101: How to Win Friends and Influence People
Slytherin 101: How to Win Friends and Influence PeopleSlytherin 101: How to Win Friends and Influence People
Slytherin 101: How to Win Friends and Influence Peopledreamwidth
 
Keeping your culture afloat through a tidal wave
Keeping your culture afloat through a tidal waveKeeping your culture afloat through a tidal wave
Keeping your culture afloat through a tidal wavedreamwidth
 
User Created Content: Maintain accessibility in content you don't control
User Created Content: Maintain accessibility in content you don't controlUser Created Content: Maintain accessibility in content you don't control
User Created Content: Maintain accessibility in content you don't controldreamwidth
 
Kicking impostor syndrome in the head
Kicking impostor syndrome in the headKicking impostor syndrome in the head
Kicking impostor syndrome in the headdreamwidth
 
Care and Feeding of Volunteers
Care and Feeding of VolunteersCare and Feeding of Volunteers
Care and Feeding of Volunteersdreamwidth
 
Sowing the Seeds of Diversity
Sowing the Seeds of DiversitySowing the Seeds of Diversity
Sowing the Seeds of Diversitydreamwidth
 
Be Kind To Your Wrists (you’ll miss them when they’re gone)
Be Kind To Your Wrists (you’ll miss them when they’re gone)Be Kind To Your Wrists (you’ll miss them when they’re gone)
Be Kind To Your Wrists (you’ll miss them when they’re gone)dreamwidth
 
Web Accessibility for the 21st Century
Web Accessibility for the 21st CenturyWeb Accessibility for the 21st Century
Web Accessibility for the 21st Centurydreamwidth
 
Servers and Processes: Behavior and Analysis
Servers and Processes: Behavior and AnalysisServers and Processes: Behavior and Analysis
Servers and Processes: Behavior and Analysisdreamwidth
 
Overcoming Impostor Syndrome
Overcoming Impostor SyndromeOvercoming Impostor Syndrome
Overcoming Impostor Syndromedreamwidth
 
Build Your Own Contributors, One Part At A Time
Build Your Own Contributors, One Part At A TimeBuild Your Own Contributors, One Part At A Time
Build Your Own Contributors, One Part At A Timedreamwidth
 

Más de dreamwidth (16)

From the Inside Out: How Self-Talk Affects Your Community
From the Inside Out: How Self-Talk Affects Your CommunityFrom the Inside Out: How Self-Talk Affects Your Community
From the Inside Out: How Self-Talk Affects Your Community
 
Chenoweth os bridge 2015 pp
Chenoweth os bridge 2015 ppChenoweth os bridge 2015 pp
Chenoweth os bridge 2015 pp
 
How We Learned To Stop Worrying And Love (or at least live with) GitHub
How We Learned To Stop Worrying And Love (or at least live with) GitHubHow We Learned To Stop Worrying And Love (or at least live with) GitHub
How We Learned To Stop Worrying And Love (or at least live with) GitHub
 
When your code is nearly old enough to vote
When your code is nearly old enough to voteWhen your code is nearly old enough to vote
When your code is nearly old enough to vote
 
Hacking In-Group Bias for Fun and Profit
Hacking In-Group Bias for Fun and ProfitHacking In-Group Bias for Fun and Profit
Hacking In-Group Bias for Fun and Profit
 
Slytherin 101: How to Win Friends and Influence People
Slytherin 101: How to Win Friends and Influence PeopleSlytherin 101: How to Win Friends and Influence People
Slytherin 101: How to Win Friends and Influence People
 
Keeping your culture afloat through a tidal wave
Keeping your culture afloat through a tidal waveKeeping your culture afloat through a tidal wave
Keeping your culture afloat through a tidal wave
 
User Created Content: Maintain accessibility in content you don't control
User Created Content: Maintain accessibility in content you don't controlUser Created Content: Maintain accessibility in content you don't control
User Created Content: Maintain accessibility in content you don't control
 
Kicking impostor syndrome in the head
Kicking impostor syndrome in the headKicking impostor syndrome in the head
Kicking impostor syndrome in the head
 
Care and Feeding of Volunteers
Care and Feeding of VolunteersCare and Feeding of Volunteers
Care and Feeding of Volunteers
 
Sowing the Seeds of Diversity
Sowing the Seeds of DiversitySowing the Seeds of Diversity
Sowing the Seeds of Diversity
 
Be Kind To Your Wrists (you’ll miss them when they’re gone)
Be Kind To Your Wrists (you’ll miss them when they’re gone)Be Kind To Your Wrists (you’ll miss them when they’re gone)
Be Kind To Your Wrists (you’ll miss them when they’re gone)
 
Web Accessibility for the 21st Century
Web Accessibility for the 21st CenturyWeb Accessibility for the 21st Century
Web Accessibility for the 21st Century
 
Servers and Processes: Behavior and Analysis
Servers and Processes: Behavior and AnalysisServers and Processes: Behavior and Analysis
Servers and Processes: Behavior and Analysis
 
Overcoming Impostor Syndrome
Overcoming Impostor SyndromeOvercoming Impostor Syndrome
Overcoming Impostor Syndrome
 
Build Your Own Contributors, One Part At A Time
Build Your Own Contributors, One Part At A TimeBuild Your Own Contributors, One Part At A Time
Build Your Own Contributors, One Part At A Time
 

Último

Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 

Último (20)

Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 

LCA2014 - Introduction to Go

  • 1. Introduction to Go a tutorial for developers Hello! While you’re sitting down, please make sure your Go environment is set up. ! Grab a USB stick that’s floating around and copy the gostick directory somewhere local. ! Then read the included README please!
  • 2. Autobiography • Mark Smith <mark@qq.is> @zorkian • Site Reliability Engineer at Dropbox • Contributes to Dreamwidth Studios • github.com/xb95
  • 3. Today’s Plan • What is Go? Where does it fit? • Obligatory Hello World • Contrived example program • Anti-unicorns • Wrap-up
  • 4. Go in a Nutshell • Designed for building systems • Statically compiled, garbage collected • Static typing (w/some duck typing) • Built-in maps (dicts) and strings
  • 5. Go in a Nutshell • Multi-core support • Concurrency primitives • Closures, etc. etc. • “Somewhere between C and Python”
  • 6. The Go Way • Go is an opinionated language • gofmt, mixedCaps, capitalization for privacy… • Simple is better than complex • The compiler should help with the heavy lifting
  • 8. Hello World package main ! import "fmt" ! func main() { fmt.Println("Hello, LCA 2014!") }
  • 9. Hello World package main ! import "fmt" ! func main() { fmt.Println("Hello, LCA 2014!") }
  • 10. Hello World package main ! import "fmt" ! func main() { fmt.Println("Hello, LCA 2014!") }
  • 11. Hello World package main ! import "fmt" ! func main() { fmt.Println("Hello, LCA 2014!") }
  • 12. Hello World package main ! import "fmt" ! func main() { fmt.Println("Hello, LCA 2014!") }
  • 13. Building Hello World # Do this in your gostick/ copy # You did follow the README? :-) cd helloworld/ go build ./helloworld
  • 14. Getting Real • Go is particularly suited for network services • The standard library is fairly comprehensive • Let’s build an echo server!
  • 15. Echo Server 1. Listen on port for TCP connections 2. Accept connections 3. Read text from connection 4. Write it back to connection
  • 16. Standard Library • Go has a decent standard library • The ecosystem is still fairly young, so it has some holes and some things aren’t well optimized • Well built for network services • http://golang.org/pkg/
  • 17. Tip: Searching When you search the Internet for information, “Go” is a bad keyword; everybody uses “golang”.
  • 18. Echo Server 1. Listen on port for TCP connections 2. Accept connections 3. Read text from connection 4. Write it back to connection
  • 19. Listening for Connections In the “net” package, you’ll find many useful things, including: func Listen(net, laddr string) (Listener, error)
  • 20. Listening for Connections In the “net” package, you’ll find many useful things, including: func Listen(net, laddr string) (Listener, error) package main ! import "net" ! func main() { ! }
  • 21. Listening for Connections In the “net” package, you’ll find many useful things, including: func Listen(net, laddr string) (Listener, error) package main ! import "net" ! func main() { ... := net.Listen("tcp", ":9000") }
  • 22. Variable Definition • Go has two ways of declaring variables, explicit and implicit • Explicit:
 var foobar uint64 • Implicit (type is inferred automatically):
 foobar := thingThatReturnsUint64() • Go strives to save on typing redundant information that the compiler can figure out
  • 23. Error Handling • Using multiple return values to get errors • Idiomatically, you will write this a lot:
 
 results, err := pkg.DoSomething(1, 2)
 if err != nil {
 log.Fatalf(“Failed: %s”, err)
 }
 // carry on and use results • Yes, this gets very verbose… oh well
  • 24. Echo Server 1. Listen on port for TCP connections 2. Accept connections 3. Read text from connection 4. Write it back to connection
  • 25. Connection Pump func Accept() (Conn, error) func main() { listener, err := net.Listen("tcp", ":9000") ! ! ! ! ! ! ! ! ! ! ! }
  • 26. Connection Pump func Accept() (Conn, error) func main() { listener, err := net.Listen("tcp", ":9000") if err != nil { panic(err) } ! ! ! ! ! ! ! ! }
  • 27. Connection Pump func Accept() (Conn, error) func main() { listener, err := net.Listen("tcp", ":9000") if err != nil { panic(err) } ! for { ! ! ! ! } }
  • 28. Connection Pump func Accept() (Conn, error) func main() { listener, err := net.Listen("tcp", ":9000") if err != nil { panic(err) } ! for { client, err := listener.Accept() ! ! ! ! } }
  • 29. Connection Pump func Accept() (Conn, error) func main() { listener, err := net.Listen("tcp", ":9000") if err != nil { panic(err) } ! for { client, err := listener.Accept() if err != nil { continue } ! } }
  • 30. Connection Pump func Accept() (Conn, error) func main() { listener, err := net.Listen("tcp", ":9000") if err != nil { panic(err) } ! for { client, err := listener.Accept() if err != nil { continue } handleClient(client) } }
  • 31. Echo Server 1. Listen on port for TCP connections 2. Accept connections 3. Read text from connection 4. Write it back to connection
  • 32. Client Handler func Read(b []byte) (int, error) func handleClient(client net.Conn) { for { // read from our client // write it back to our client } }
  • 33. Client Handler func Read(b []byte) (int, error) func handleClient(client net.Conn) { for { // read from our client // write it back to our client } } Okay, so what’s a []byte?
  • 34. Primitive Go Types • All of the usual suspects (ints, uints, floats) • Built-in string type (Unicode, immutable) • int and uint are architecture-width • byte is just a synonym for uint8
  • 35. More Types • arrays: of a declared, fixed length • slice: a segment (“slice”) of an array • map: key/value storage • pointer: much like C (uses & and *) • (more to come later)
  • 36. So, []byte… • Let’s make an array of bytes:
 var ary [4096]byte • What if we don’t know the size? Or don’t care? Slices solve this problem:
 var aryslice []byte • This is great, but what is it?
  • 37. Slices Explained var a [16]byte is
  • 38. Slices Explained var a [16]byte is a[3] is
  • 39. Slices Explained var a [16]byte is a[3] is a[6:8] is
  • 40. Slices Explained var a [16]byte is a[3] is a[6:8] is var s []byte is nothing to start with.
  • 41. Slices Explained var a [16]byte is a[3] is a[6:8] is var s []byte is nothing to start with. s = a[6:8]; s is
  • 42. Slices Explained var a [16]byte is a[3] is a[6:8] is var s []byte is nothing to start with. s = a[6:8]; s is s[0] is the same as a[6], etc! A slice is simply a window into a backing array.
  • 43. Slices pt. 2 • Slices are internally a tuple of (array, start, length) • A slice can be moved around (a sliding window) and resized (limited to the backing array size) • Slices are reference types; great for passing to functions
  • 44. Echo Server 1. Listen on port for TCP connections 2. Accept connections 3. Read text from connection 4. Write it back to connection
  • 45. Client Handler func Read(b []byte) (int, error) func handleClient(client net.Conn) { for { ! ! ! ! ! ! } }
  • 46. Client Handler func Read(b []byte) (int, error) func handleClient(client net.Conn) { for { buf := make([]byte, 4096) ! ! ! ! } }
  • 47. Client Handler func Read(b []byte) (int, error) func handleClient(client net.Conn) { for { buf := make([]byte, 4096) numbytes, err := client.Read(buf) ! ! ! ! } }
  • 48. Client Handler func Read(b []byte) (int, error) func handleClient(client net.Conn) { for { buf := make([]byte, 4096) numbytes, err := client.Read(buf) if numbytes == 0 || err != nil { return } ! } }
  • 49. Client Handler func Read(b []byte) (int, error) func handleClient(client net.Conn) { for { buf := make([]byte, 4096) numbytes, err := client.Read(buf) if numbytes == 0 || err != nil { return } client.Write(buf) } }
  • 50. Build & Test # Do this in your gostick/ copy cd part1/ go build ./part1 & telnet localhost 9000 # say something and hit enter
  • 51. More Power • The echo server is great, but can only serve one user at a time! • Concurrent network programming… should we fork for each child? use a threading library? maybe we can implement it using non-blocking I/O… • Stop, stop, stop!
  • 52. Concurrent, Go Style Remember our main accept loop? Let’s tweak it from this… func main() { listener, err := net.Listen("tcp", ":9000") if err != nil { panic(err) } ! for { client, err := listener.Accept() if err != nil { continue } handleClient(client) } }
  • 53. Concurrent, Go Style …to this! func main() { listener, err := net.Listen("tcp", ":9000") if err != nil { panic(err) } ! for { client, err := listener.Accept() if err != nil { continue } go handleClient(client) } }
  • 54. Goroutines • A goroutine is a function executing concurrently with other goroutines • Go multiplexes M goroutines onto N processes, and scales to 100,000+ goroutines (millions possible, use case dependent) • N is by default only 1, you can tune it
  • 55. Goroutines pt. 2 • Everything in Go is designed to be blocking, in essence, and the idea is to use goroutines • This makes reasoning about software much easier • Go also provides deadlock detection and backtraces all living goroutines
  • 56. Echo v2.0, “Chat” • Let’s make a chat server out of our echo server • Design goal: any text from one client is echoed immediately to all connected clients • Well, clearly, our goroutines have to communicate
  • 57. Communication • The common idiom for communicating between threads in most languages is shared memory • This kind of work is notoriously racy, error prone, and hard to implement correctly
  • 58. Communication in Go • “Don’t communicate by sharing memory; share memory by communicating!” • Enter the concept of channels • A built-in goroutine safe method of passing data
  • 59. Basic Channel Use ch := make(chan int) Create a new channel ch <- 5 Write to a channel Read from a channel i := <-ch The channel in this example is an unbuffered channel. Reads block until data is available, writes block until someone reads. Synchronous.
  • 60. Buffered Channels ch := make(chan int, 10) Create a new channel ch <- 5 Write to a channel Read from a channel i := <-ch The difference: buffered channels won’t block when inserting data (unless they’re full).
  • 61. Chat Server • We have to make three main modifications: 1. Store a list of connected clients 2. Get input out of the clients 3. Write each input back to every client • Note: this implementation is a reduced example, so it’s a little unsafe in one place :-)
  • 62. Chat Server • We have to make three main modifications: 1. Store a list of connected clients 2. Get input out of the clients 3. Write each input back to every client • Note: this implementation is a reduced example, so it’s a little unsafe in one place :-)
  • 63. Connected Clients func main() { ... ! ! ! for { client, err := listener.Accept() if err != nil { continue } ! go handleClient(client) } }
  • 64. Connected Clients func main() { ... ! var clients []net.Conn ! for { client, err := listener.Accept() if err != nil { continue } clients = append(clients, client) go handleClient(client) } } Build a slice of clients and then append each new client to the slice. ! The append built-in handles automatically allocating and growing the backing array as necessary.
  • 65. Chat Server • We have to make three main modifications: 1. Store a list of connected clients 2. Get input out of the clients 3. Write each input back to every client • Note: this implementation is a reduced example, so it’s a little unsafe in one place :-)
  • 66. Getting Input func main() { ... ! for { ... clients = append(clients, client) go handleClient(client) } } ! func handleClient(client net.Conn) { for { ... client.Write(buf) } }
  • 67. Getting Input func main() { ... input := make(chan []byte, 10) ! for { ... clients = append(clients, client) go handleClient(client) } } ! func handleClient(client net.Conn) { for { ... client.Write(buf) } }
  • 68. Getting Input func main() { ... input := make(chan []byte, 10) ! for { ... clients = append(clients, client) go handleClient(client, input) } } ! func handleClient(client net.Conn) { for { ... client.Write(buf) } }
  • 69. Getting Input func main() { ... input := make(chan []byte, 10) ! for { ... clients = append(clients, client) go handleClient(client, input) } } ! func handleClient(client net.Conn, input chan []byte) { for { ... client.Write(buf) } }
  • 70. Getting Input func main() { ... input := make(chan []byte, 10) ! for { ... clients = append(clients, client) go handleClient(client, input) } } ! func handleClient(client net.Conn, input chan []byte) { for { ... client.Write(buf) input <- buf } }
  • 71. Getting Input func main() { ... input := make(chan []byte, 10) ! for { ... clients = append(clients, client) go handleClient(client, input) } } ! func handleClient(client net.Conn, input chan []byte) { for { ... client.Write(buf) Removed the Write! input <- buf } }
  • 72. What is Happening? • handleClient still is a blocking read loop, but instead of writing back to the client it writes the bytes onto a channel • main now has a list of all clients and a channel that everybody is writing input to • Final piece: somebody to read from the channel!
  • 73. Chat Server • We have to make three main modifications: 1. Store a list of connected clients 2. Get input out of the clients 3. Write each input back to every client • Note: this implementation is a reduced example, so it’s a little unsafe in one place :-)
  • 74. The Chat Manager func main() { ... var clients []net.Conn input := make(chan []byte, 10) ! ! ! ! We’ve ! ! ! ! ... } seen this all before…
  • 75. The Chat Manager func main() { ... var clients []net.Conn input := make(chan []byte, 10) go func() { ! ! ! ! ! }() ... } You can create goroutines out of closures, too. !
  • 76. The Chat Manager ! func main() { ... var clients []net.Conn input := make(chan []byte, 10) go func() { for { message := <-input ! ! } }() ... } ! ! This is all blocking!
  • 77. The Chat Manager ! func main() { ... var clients []net.Conn This input := make(chan []byte, 10) go func() { for { message := <-input for _, client := range clients { ! } } }() ... } ! ! is all blocking!
  • 78. range and _ for _, client := range clients {..} • The range keyword iterates over maps/slices/ arrays and returns the key (or index) and value • Underscore (_) is the anonymous variable (“blank identifier”), you can use it to discard results
  • 79. The Chat Manager func main() { ... var clients []net.Conn input := make(chan []byte, 10) go func() { for { message := <-input for _, client := range clients { client.Write(message) } } }() ... }
  • 80. Build & Test # Do this in your gostick/ copy cd part2/ go build ./part2 & telnet localhost 9000 # say something and hit enter, now connect # again in another window and chat! :-)
  • 81. Echo 3.0, “Frotzer” • We’re entering the land of extremely contrived tutorial examples, but… • Let’s give our chat server some behaviors! • When a user chats, we apply some formatting rules before sending it out to other users
  • 82. But First: More Type Talk! • Go doesn’t have classes (or objects, really) • However, you have named types, and methods are attached to named types:
 
 
 
 type Uppercaser struct{} ! func (self Uppercaser) Frotz(input []byte) []byte { return bytes.ToUpper(input) }
  • 83. Um, struct{}? • Go supports structs, very much like any other language that has structs • The empty struct is often used for hanging methods • Instantiating a struct type:
 foobar := Lowercaser{}
 foobar.Frotz(“HELLO”) // “hello”
  • 84. Interfaces • In Go, an interface specifies a collection of methods attached to a name • Interfaces are implicit (duck typed)
 
 
 
 type Frotzer interface { Frotz([]byte) []byte } ! type Uppercaser struct{} func (self Uppercaser) Frotz(input []byte) []byte { return bytes.ToUpper(input) }
  • 85. Implementing Frotzing func chatManager(clients *[]net.Conn, input chan []byte ) { for { message := <-input for _, client := range *clients { client.Write(message) } } } This is the input handler loop we built a few minutes ago, except now we pulled it out of main and made it a function.
  • 86. Implementing Frotzing func chatManager(clients *[]net.Conn, input chan []byte, frotz Frotzer) { for { message := <-input for _, client := range *clients { client.Write(frotz.Frotz(message)) } } } Now it takes a third argument: an interface. Any type that implements Frotzer can be used!
  • 87. Making Frotzers These are both named types, and both implement (implicitly!) the Frotzer interface. Thanks, compiler! type Uppercaser struct{} func (self Uppercaser) Frotz(input []byte) []byte { return bytes.ToUpper(input) } ! type Lowercaser struct{} func (self Lowercaser) Frotz(input []byte) []byte { return bytes.ToLower(input) }
  • 88. A New Main func main() { ... var clients []net.Conn input := make(chan []byte, 10) go chatManager(&clients, input) ! ! for { client, err := listener.Accept() if err != nil { continue } clients = append(clients, client) go handleClient(client, input) } }
  • 89. A New Main func main() { ... var clients []net.Conn input := make(chan []byte, 10) go chatManager(&clients, input, Lowercaser{}) go chatManager(&clients, input, Uppercaser{}) ! for { client, err := listener.Accept() if err != nil { continue } clients = append(clients, client) go handleClient(client, input) } }
  • 90. A New Main pt. 2 • The chatManager functions are spawned into separate goroutines • Channels are goroutine safe, so they end up interleaving reads (not guaranteed!) • chatManager doesn’t know what you’re passing it as a Frotzer, it just knows it can call Frotz on it
  • 91. Build & Test # Do this in your gostick/ copy cd part3/ go build ./part3 & telnet localhost 9000 # say something and hit enter, your text # should alternate UPPER/lower
  • 92. Review • A multi-user chat server demonstrating many core parts of the Go programming language • Built-in concurrency that is easy to use and trivially allows relatively powerful constructions • A classless, statically type system that still enables many OO concepts
  • 93. Go is Awesome :-) This slide should have unicorns and bunnies on it
  • 94. Things That Aren’t Unicorns Yet • The GC is generally pretty solid, but if you are doing many allocations and need performance, you have to do the usual tricks • Standard library is pretty young and not optimized • Overall ecosystem is small (but growing, hi!)
  • 95. Also: The Scheduler • It’s new and has room for improvement • Heavy use of channels between goroutines is probably faster in one process • You’ll have to play with GOMAXPROCS and understand your application
  • 96. Topics Undiscussed • Building your own packages (it’s easy) • Importing from external repositories
 import pcap “github.com/akrennmair/gopcap” • Using gofmt (do it!) • switch, select, etc. • Type assertions, reflection, even more etc.
  • 97. Thank you! Mark Smith <mark@qq.is> @zorkian
 SRE at Dropbox (we’re hiring!) • Some recommended reading:
 Effective Go golang.org/doc/effective_go.html
 FAQ golang.org/doc/faq • Many more talks and presentations:
 code.google.com/p/go-wiki/wiki/GoTalks • The Go Playground! play.golang.org