Go(lang) for the Rubyist

Preview:

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

@markbates

@markbates

LARUBY2014www.metacasts.tv

GoElixir

Erlang

Node

Scala

Clojure

CoffeeScript

JavaScript

#WTF??

“Art isn’t created in a vacuum.”

neither is!SOFTWARE!

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.

What is Go?

pro-tip: golang

40% of the Internet

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

compiled

statically typed

garbage collected

cross platform

concurrent

simple

25 keywords

no exceptions

Basics

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

Concurrency

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

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

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

Speed

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())!! }!}

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

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

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

Duck Typing?

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)!}

Package Management

go get

Testing

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

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))! })! })!})

Web

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)!}

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

Database

PostgresSQLite

MySql

Sybase

DB2

MS ADODB

ODBC

Oracle

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

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

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

Workers

Assets

Lineman.js :)

When to Use Go

cli

concurrency #ftw

speed

light-moderate!learning curve

shell out to ruby

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

@markbateshttp://www.metacasts.tv