Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go: A Pragmatic Language

Go: A Pragmatic Language

When I first looked at Go, the open source programming language developed at Google, I was unimpressed. Then I used it. Now I find Go to be a solid and useful tool in my toolchest of languages. In this talk I'll introduce Go, its syntax, how you might use it. I'll talk about its strengths and weaknesses and give you code examples demonstrating some of its finer points. I will also show you some examples of how we use Go within DNSimple, in production. So join me for an intro to the world of Go, and learn about how to use this pragmatic language to solve your programming challenges.

Anthony Eden

June 05, 2014
Tweet

More Decks by Anthony Eden

Other Decks in Programming

Transcript

  1. prag·mat·ic - “dealing with things sensibly and realistically in a

    way that is based on practical rather than theoretical considerations.”
  2. “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.”
  3. “Go is more about software engineering than programming language research.

    Or to rephrase, it is about language design in the service of software engineering.”
  4. 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))" }
  5. 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)" }" }
  6. 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" }" }
  7. 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" }" }
  8. 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]
  9. i := 0" for {" fmt.Printf("%v: %v\n", i, a[i])" i

    += 1" if i >= len(a) {" break" }" }
  10. f := func(x int, y int) int {" return x

    + y" }" fmt.Printf("Function result: %v\n", f(10, 22))
  11. 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))
  12. “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.”
  13. import (" "encoding/json"" "fmt"" " "log"" "net"" "net/http"" "os"" )

    gapl "github.com/aeden/go-a-pragmatic-language-lib"
  14. 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)
  15. func personHandler(w http.ResponseWriter, r *http.Request) {" " ! ! !

    } w.Header().Set( person encoder encoder.Encode(person)
  16. 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)
  17. 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)" }" }"
  18. 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" }" }
  19. 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())
  20. 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" }
  21. 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)" }
  22. 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
  23. “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.”
  24. • 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?