63
@markbates

Go(lang) for the Rubyist

  • Upload
    mark

  • View
    4.824

  • Download
    3

Embed Size (px)

DESCRIPTION

Presented at LA Ruby Conf on February 8th, 2014. A high level overview of Go, golang, and how it relates to Ruby. Packages such as Martini, Lineman.js, sqlx, Ginkgo, and more are covered.

Citation preview

Page 1: Go(lang) for the Rubyist

@markbates

Page 2: Go(lang) for the Rubyist

@markbates

Page 3: Go(lang) for the Rubyist
Page 4: Go(lang) for the Rubyist
Page 5: Go(lang) for the Rubyist
Page 6: Go(lang) for the Rubyist

LARUBY2014www.metacasts.tv

Page 7: Go(lang) for the Rubyist
Page 8: Go(lang) for the Rubyist

GoElixir

Erlang

Node

Scala

Clojure

CoffeeScript

JavaScript

Page 9: Go(lang) for the Rubyist

#WTF??

Page 10: Go(lang) for the Rubyist

“Art isn’t created in a vacuum.”

Page 11: Go(lang) for the Rubyist

neither is!SOFTWARE!

Page 12: Go(lang) for the Rubyist

pol·y·glot (pŏl′ē-glŏt′)!adj. Speaking, writing, written in, or composed of several languages. n. 1. A person having a speaking, reading, or writing knowledge of several languages. 2. A book, especially a Bible, containing several versions of the same text in different languages. 3. A mixture or confusion of languages.

Page 13: Go(lang) for the Rubyist
Page 14: Go(lang) for the Rubyist

What is Go?

Page 15: Go(lang) for the Rubyist
Page 16: Go(lang) for the Rubyist

pro-tip: golang

Page 17: Go(lang) for the Rubyist

40% of the Internet

http://www.forbes.com/sites/timworstall/2013/08/17/fascinating-number-google-is-now-40-of-the-internet/

Page 18: Go(lang) for the Rubyist

compiled

Page 19: Go(lang) for the Rubyist

statically typed

Page 20: Go(lang) for the Rubyist

garbage collected

Page 21: Go(lang) for the Rubyist

cross platform

Page 22: Go(lang) for the Rubyist

concurrent

Page 23: Go(lang) for the Rubyist

simple

Page 24: Go(lang) for the Rubyist

25 keywords

Page 25: Go(lang) for the Rubyist

no exceptions

Page 26: Go(lang) for the Rubyist

Basics

Page 27: Go(lang) for the Rubyist

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

Page 28: Go(lang) for the Rubyist

Concurrency

Page 29: Go(lang) for the Rubyist

package main!!func LiftHeavyStuff() {! // do heavy lifting!}!!func main() {! LiftHeavyStuff()!}

Page 30: Go(lang) for the Rubyist

package main!!func LiftHeavyStuff() {! // do heavy lifting!}!!func main() {! go LiftHeavyStuff()!}

Page 31: Go(lang) for the Rubyist

package main!!import "fmt"!!func main() {!!! comm := make(chan string)!! go func() {!! ! // do heavy lifting!! ! comm <- "Done heave lifting"!! }()!!! fmt.Println(<-comm)!}

Page 32: Go(lang) for the Rubyist

Speed

Page 33: Go(lang) for the Rubyist

package main!!import "fmt"!!func fibonacci() func() int {!! x := 0!! y := 1!! return func() int {!! ! x, y = y, x+y!! ! return x!! }!}!!func main() {!! f := fibonacci()!! for i := 0; i < 10; i++ {!! ! fmt.Println(f())!! }!}

Page 34: Go(lang) for the Rubyist

def fibonacci(n)! return n if n <= 1! fibonacci(n - 1) + fibonacci(n - 2)!end!!puts fibonacci( 10 )

Page 35: Go(lang) for the Rubyist

$ go build$ time ./fibreal 0m0.005suser 0m0.001ssys 0m0.003s

$ time ruby fib.rbreal 0m0.038suser 0m0.029ssys 0m0.007s

Page 36: Go(lang) for the Rubyist

Duck Typing?

Page 37: Go(lang) for the Rubyist

package main!!import "fmt"!!type Greeter interface {! SayHello()!}!!func Talk(g Greeter) {! g.SayHello()!}!!type Foo struct {! x int!}!!func (f Foo) SayHello() {! fmt.Println("[Foo] Hello!")!}

type Bar struct {! z string!}!!func (b Bar) SayHello() {! fmt.Println("[Bar] Hello!")!}!!func main() {! foo := Foo{}! Talk(foo)! bar := Bar{}! Talk(bar)!}

Page 38: Go(lang) for the Rubyist

Package Management

Page 39: Go(lang) for the Rubyist

go get

Page 40: Go(lang) for the Rubyist
Page 41: Go(lang) for the Rubyist

Testing

Page 42: Go(lang) for the Rubyist

package calculator!!import "testing"!!func TestAdd(t *testing.T) {! aresult := Add(1, 2)! if aresult != 3 {! t.Errorf("Expected %d, Actual %d", 3, aresult)! }!}

Page 43: Go(lang) for the Rubyist
Page 44: Go(lang) for the Rubyist

package calculator_test!!import (! . "calculator"! . "github.com/onsi/ginkgo"! . "github.com/onsi/gomega"!)!!var _ = Describe("Calculator", func() {! Describe("Add", func() {! It("Adds two numbers together", func() {! Expect(Add(1, 2)).To(Equal(3))! })! })!})

Page 45: Go(lang) for the Rubyist

Web

Page 46: Go(lang) for the Rubyist

package main!!import (! "fmt"! "net/http"!)!!func handler(w http.ResponseWriter, r *http.Request) {! fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])!}!!func main() {! http.HandleFunc("/", handler)! http.ListenAndServe(":8080", nil)!}

Page 47: Go(lang) for the Rubyist
Page 48: Go(lang) for the Rubyist

package main!!import "github.com/codegangsta/martini"!!func main() {! m := martini.Classic()! m.Get("/", func() string {! return "Hello world!"! })! m.Run()!}

Page 49: Go(lang) for the Rubyist

Database

Page 50: Go(lang) for the Rubyist

PostgresSQLite

MySql

Sybase

DB2

MS ADODB

ODBC

Oracle

Page 51: Go(lang) for the Rubyist

SQLX - https://github.com/jmoiron/sqlx

GORP - https://github.com/coopernurse/gorp

JET - https://github.com/eaigner/jet

Page 52: Go(lang) for the Rubyist

Workers

Page 53: Go(lang) for the Rubyist
Page 54: Go(lang) for the Rubyist

Assets

Page 55: Go(lang) for the Rubyist

Lineman.js :)

Page 56: Go(lang) for the Rubyist

When to Use Go

Page 57: Go(lang) for the Rubyist

cli

Page 58: Go(lang) for the Rubyist

concurrency #ftw

Page 59: Go(lang) for the Rubyist

speed

Page 60: Go(lang) for the Rubyist

light-moderate!learning curve

Page 61: Go(lang) for the Rubyist

shell out to ruby

Page 62: Go(lang) for the Rubyist

Resources

http://golang.org/

http://golang.org/doc/effective_go.html

http://www.metacasts.tv/casts/intro-to-go

http://www.metacasts.tv/casts/testing-go

http://www.metacasts.tv/casts/sql-and-go

Page 63: Go(lang) for the Rubyist

@markbateshttp://www.metacasts.tv