Go(lang) for the Rubyist

5,776 views
5,500 views

Published on

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.

Published in: Technology, Design
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,776
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
40
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Go(lang) for the Rubyist

  1. 1. @markbates
  2. 2. @markbates
  3. 3. www.metacasts.tv LARUBY2014
  4. 4. Erlang Node CoffeeScript Scala Clojure Go Elixir JavaScript
  5. 5. #WTF??
  6. 6. “Art isn’t created in a vacuum.”
  7. 7. neither is! SOFTWARE!
  8. 8. 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.
  9. 9. What is Go?
  10. 10. pro-tip: golang
  11. 11. 40% of the Internet http://www.forbes.com/sites/timworstall/2013/08/17/fascinating-number-google-is-now-40-of-the-internet/
  12. 12. compiled
  13. 13. statically typed
  14. 14. garbage collected
  15. 15. cross platform
  16. 16. concurrent
  17. 17. simple
  18. 18. 25 keywords
  19. 19. no exceptions
  20. 20. Basics
  21. 21. package main! ! import "fmt"! ! func main() {! fmt.Println("Hello, World!")! }
  22. 22. Concurrency
  23. 23. package main! ! func LiftHeavyStuff() {! // do heavy lifting! }! ! func main() {! LiftHeavyStuff()! }
  24. 24. package main! ! func LiftHeavyStuff() {! // do heavy lifting! }! ! func main() {! go LiftHeavyStuff()! }
  25. 25. package main! ! import "fmt"! ! func main() {! ! ! ! ! ! ! comm := make(chan string)! go func() {! ! // do heavy lifting! ! comm <- "Done heave lifting"! }()! ! ! fmt.Println(<-comm)! }
  26. 26. Speed
  27. 27. 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())! ! }! }
  28. 28. def fibonacci(n)! return n if n <= 1! fibonacci(n - 1) + fibonacci(n - 2)! end! ! puts fibonacci( 10 )
  29. 29. $ go build $ time ./fib real 0m0.005s user 0m0.001s sys 0m0.003s $ time ruby fib.rb real 0m0.038s user 0m0.029s sys 0m0.007s
  30. 30. Duck Typing?
  31. 31. package main! import "fmt"! type Bar struct {! z string! }! ! ! type Greeter interface {! SayHello()! }! func (b Bar) SayHello() {! fmt.Println("[Bar] Hello!")! }! ! ! func Talk(g Greeter) {! g.SayHello()! }! func main() {! foo := Foo{}! Talk(foo)! bar := Bar{}! Talk(bar)! } ! ! type Foo struct {! x int! }! ! func (f Foo) SayHello() {! fmt.Println("[Foo] Hello!")! }
  32. 32. Package Management
  33. 33. go get
  34. 34. Testing
  35. 35. package calculator! ! import "testing"! ! func TestAdd(t *testing.T) {! aresult := Add(1, 2)! if aresult != 3 {! t.Errorf("Expected %d, Actual %d", 3, aresult)! }! }
  36. 36. 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))! })! })! })
  37. 37. Web
  38. 38. 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)! }
  39. 39. package main! ! import "github.com/codegangsta/martini"! ! func main() {! m := martini.Classic()! m.Get("/", func() string {! return "Hello world!"! })! m.Run()! }
  40. 40. Database
  41. 41. MySql Sybase ODBC DB2 Postgres MS ADODB SQLite Oracle
  42. 42. SQLX - https://github.com/jmoiron/sqlx GORP - https://github.com/coopernurse/gorp JET - https://github.com/eaigner/jet
  43. 43. Workers
  44. 44. Assets
  45. 45. Lineman.js :)
  46. 46. When to Use Go
  47. 47. cli
  48. 48. concurrency #ftw
  49. 49. speed
  50. 50. light-moderate! learning curve
  51. 51. shell out to ruby
  52. 52. 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
  53. 53. @markbates http://www.metacasts.tv

×