Slide 1

Slide 1 text

Go: A Pragmatic Language Anthony Eden - @aeden - DNSimple

Slide 2

Slide 2 text

prag·mat·ic - “dealing with things sensibly and realistically in a way that is based on practical rather than theoretical considerations.”

Slide 3

Slide 3 text

“Go's purpose is not to do research into programming language design; it is to improve the working environment for its designers and their coworkers.”

Slide 4

Slide 4 text

What is Go?

Slide 5

Slide 5 text

Systems Language

Slide 6

Slide 6 text

Strongly Typed

Slide 7

Slide 7 text

Garbage Collected

Slide 8

Slide 8 text

Explicit support for Concurrent Programming

Slide 9

Slide 9 text

“Go is more about software engineering than programming language research. Or to rephrase, it is about language design in the service of software engineering.”

Slide 10

Slide 10 text

Learning by Example

Slide 11

Slide 11 text

HTTP Server

Slide 12

Slide 12 text

package main import ) func fmt.Fprintf(w, } func http.HandleFunc( " log.Fatal(http.ListenAndServe( }

Slide 13

Slide 13 text

package main import (" "fmt"" "log"" "net/http"" ) func rootHandler(w http.ResponseWriter, r *http.Request) {" fmt.Fprintf(w, "All systems go\n")" }" " func main() {" http.HandleFunc("/", rootHandler)" " log.Fatal(http.ListenAndServe(":8080", nil))" }

Slide 14

Slide 14 text

HTTP Client

Slide 15

Slide 15 text

package main import ) func resp, err log.Printf( } log.Printf( }" }

Slide 16

Slide 16 text

package main import (" "log"" "net/http"" ) func main() {" resp, err := http.Get("http://localhost:8080/")" if err != nil {" log.Printf("Error: %s", err)" } else {" log.Printf("%v", resp)" }" }

Slide 17

Slide 17 text

Initialization: Constants, Variables and init()

Slide 18

Slide 18 text

const DEFAULT_HTTP_BIND_PORT = "8080"" " var (! httpBindAddress = os.Getenv("HTTP_BIND_ADDRESS")" httpBindPort = os.Getenv("HTTP_BIND_PORT")" )" ! func init() {" if httpBindPort == "" {" httpBindPort = DEFAULT_HTTP_BIND_PORT" }" }

Slide 19

Slide 19 text

package " import )" ! ! ! ! ! ! ! ! ! ! ! ! ! ! func rootHandler(w http.ResponseWriter, r fmt.Fprintf(w, }" " func main() {" http.HandleFunc( ! hostAndPort log.Fatal(http.ListenAndServe(hostAndPort, } const DEFAULT_HTTP_BIND_PORT = "8080"" " var (! httpBindAddress = os.Getenv("HTTP_BIND_ADDRESS")" httpBindPort = os.Getenv("HTTP_BIND_PORT")" )" ! func init() {" if httpBindPort == "" {" httpBindPort = DEFAULT_HTTP_BIND_PORT" }" }

Slide 20

Slide 20 text

Types

Slide 21

Slide 21 text

Slices

Slide 22

Slide 22 text

a := []int{1, 2, 3, 4, 5} // [1 2 3 4 5] s := a[2:4] // [3 4] s = a[:3] // [1 2 3] s = a[1:] // [2 3 4 5] s = a[:] // [1 2 3 4 5]

Slide 23

Slide 23 text

Looping

Slide 24

Slide 24 text

i := 0" for {" fmt.Printf("%v: %v\n", i, a[i])" i += 1" if i >= len(a) {" break" }" }

Slide 25

Slide 25 text

for index, value := range a {" fmt.Printf("%v: %v\n", index, value)" }

Slide 26

Slide 26 text

Functions

Slide 27

Slide 27 text

func rootHandler(w http.ResponseWriter, r *http.Request) {" fmt.Fprintf(w, "All systems go\n")" }

Slide 28

Slide 28 text

f := func(x int, y int) int {" return x + y" }" fmt.Printf("Function result: %v\n", f(10, 22))

Slide 29

Slide 29 text

func main() {" a := []int{1, 2, 3, 4, 5}" ! ! ! ! ! ! ! ! ! } multiplier := func(m int) []int {" r := []int{}" for _, v := range a {" r = append(r, v*m)" }" return r" }" " func closure(f func(int) []int) []int {" return f(10)" } fmt.Printf("Closure result: %v\n", closure(multiplier))

Slide 30

Slide 30 text

“Go is an attempt to combine the ease of programming of an interpreted, dynamically typed language with the efficiency and safety of a statically typed, compiled language.”

Slide 31

Slide 31 text

go build server.go

Slide 32

Slide 32 text

go run server.go

Slide 33

Slide 33 text

Dependency Management

Slide 34

Slide 34 text

package gapl" " type Person struct {" Name string" Age int" }

Slide 35

Slide 35 text

import (" "encoding/json"" "fmt"" " "log"" "net"" "net/http"" "os"" ) gapl "github.com/aeden/go-a-pragmatic-language-lib"

Slide 36

Slide 36 text

import ) gapl "github.com/aeden/go-a-pragmatic-language-lib"

Slide 37

Slide 37 text

func personHandler(w http.ResponseWriter, r *http.Request) {" " ! ! ! } w.Header().Set("Content-type", "application/json") person := gapl.Person{Name: "John Smith", Age: 42} encoder := json.NewEncoder(w) encoder.Encode(person)

Slide 38

Slide 38 text

func personHandler(w http.ResponseWriter, r *http.Request) {" " ! ! ! } w.Header().Set( person encoder encoder.Encode(person)

Slide 39

Slide 39 text

package " import ! )" " const DEFAULT_HTTP_BIND_PORT = " var (" httpBindAddress = os.Getenv( httpBindPort = os.Getenv( )" " func init() {" }" " func rootHandler(w http.ResponseWriter, r fmt.Fprintf(w, }" " ! ! ! ! ! ! " func main() {" http.HandleFunc( ! " hostAndPort log.Fatal(http.ListenAndServe(hostAndPort, } gapl "github.com/aeden/go-a-pragmatic-language-lib" func personHandler(w http.ResponseWriter, r *http.Request) {" w.Header().Set("Content-type", "application/json")" person := gapl.Person{Name: "John Smith", Age: 42}" encoder := json.NewEncoder(w)" encoder.Encode(person)" } http.HandleFunc("/person", personHandler)

Slide 40

Slide 40 text

Concurrency

Slide 41

Slide 41 text

Communicating Sequential Processes

Slide 42

Slide 42 text

Message Passing Via Channels

Slide 43

Slide 43 text

package main" " import (" "fmt"" "math/rand"" "time"" )

Slide 44

Slide 44 text

func addRand(c chan int) {" for {" time.Sleep(1 * time.Second)" c <- rand.Intn(100)" }" }" " func subRand(c chan int) {" for {" time.Sleep(2 * time.Second)" c <- -rand.Intn(100)" }" }"

Slide 45

Slide 45 text

func printSum(c chan int) {" sum := 0" val := 0" " for {" val = <-c" fmt.Printf("%v + %v = %v\n", sum, val, sum+val)" sum = sum + val" }" }

Slide 46

Slide 46 text

func main() {" " " " " " ! ! ! ! " " } quitChan := make(chan int)" ! ! ! ! ! ! ! <-quitChan c := make(chan int)" " go printSum(c)" go addRand(c)" go subRand(c) rand.Seed(time.Now().Unix())

Slide 47

Slide 47 text

package main" " import (" " "fmt"" " "math/rand"" " "time"" )" " func addRand(c chan int) {" " for {" " " time.Sleep(1 * time.Second)" " " c <- rand.Intn(100)" " }" }" " func subRand(c chan int) {" " for {" " " time.Sleep(2 * time.Second)" " " c <- -rand.Intn(100)" " }" }" " func printSum(c chan int) {" " sum := 0" " val := 0" " " for {" " " val = <-c" " " fmt.Printf("%v + %v = %v\n", sum, val, sum+val)" " " sum = sum + val" " }" }" " func main() {" " rand.Seed(time.Now().Unix())" " " quitChan := make(chan int)" " " c := make(chan int)" " " go printSum(c)" " go addRand(c)" " go subRand(c)" " " <-quitChan" }

Slide 48

Slide 48 text

Interfaces

Slide 49

Slide 49 text

Test

Slide 50

Slide 50 text

package gapl import (" "testing"" ) func TestInMemoryPersonStore(t *testing.T) {" ! ! ! ! ! ! ! ! ! } name := "John Smith"" person := Person{Name: name, Age: 30} store := NewInMemoryPersonStore()" store.Put(name, person) person = store.Get(name)" if person.Name != name {" t.Errorf("Expected %s, got %s", name, person.Name)" }

Slide 51

Slide 51 text

Implementation

Slide 52

Slide 52 text

type PersonStore interface {" " Get(name string) Person" " Put(name string, person Person)" } type inMemoryPersonStore struct {" " people map[string]Person" }" func (store *inMemoryPersonStore) Get(name string) Person {" " return store.people[name]" }" " func (store *inMemoryPersonStore) Put(name string, person Person) {" " store.people[name] = person" } func NewInMemoryPersonStore() PersonStore {" " return &inMemoryPersonStore{people: make(map[string]Person)}" } package gapl

Slide 53

Slide 53 text

Where Is Go Useful?

Slide 54

Slide 54 text

Network Services

Slide 55

Slide 55 text

System Monitoring Agents

Slide 56

Slide 56 text

Command-line Utilities

Slide 57

Slide 57 text

Data Processing

Slide 58

Slide 58 text

Let’s See it Work

Slide 59

Slide 59 text

“Go is an attempt to combine the ease of programming of an interpreted, dynamically typed language with the efficiency and safety of a statically typed, compiled language.”

Slide 60

Slide 60 text

• What systems do I already control that might benefit from Go’s design? • What can I learn from Go to take to my preferred programming environment? • What is missing from Go that I absolutely cannot live without, and why?

Slide 61

Slide 61 text

Go: A Pragmatic Language Anthony Eden - @aeden - DNSimple