9. Go
•
Initially developed at Google
•
Created by Ken Thompson (Unix), Rob Pike
(Plan 9), and Russ Cox (libtask)
•
Development started in 2007
•
First release in 2009 (Fairly new!)
10. Go is an open source
programming environment
that makes it easy to build
simple, reliable, and
efficient software.
Source: golang.org
21. User defined types
package main;
type Animal struct {
Name string
Age int
}
func main() {
var anaconda Animal
}
22. Compiler
•
Uses GCC as back end
•
Checks for unused packages and variables
•
Checks types and return values
23. Go tools
•
go fmt -> format your source code (cool!)
•
go get -> manage and install your
dependencies
•
go build / run -> compile and run your
program
•
go test -> run your tests
31. Objects in Ruby
•
Ruby Object model
•
Module / Class with methods
•
Support for inheritance and composition
•
Everything is an object
32. class Animal
attr_accessor :name
def initialize(name)
self.name = name
end
def say_something
puts "HEY HEY"
end
end
!
a = Animal.new("Duck")
a.say_something
33. Objects in Go
•
No inheritance
•
Data types to define content
•
Methods define the operations
•
Interfaces define the behavior of the
“object” (data type)
34. package main
import "fmt"
type Animal struct {
Name string
}
!
func (a *Animal) SaySomething() {
fmt.Println("HEY HEY")
}
!
func main() {
a := new(Animal)
a.Name = "Duck"
a.SaySomething()
}
35. Error Handling
•
Ruby uses Exceptions, with begin / rescue /
ensure
•
Golang use return values with error code (!!!)
36. Error handling - Ruby
•
Errors inherit from Exception class
•
Flow control with begin / rescue / ensure
•
Don’t need to treat every exception
38. Error Handling - Go
•
No exceptions
•
Errors should implement “Error” interface
•
Erros should be returned as additional value
•
Every error should be treated (unless you want
to do a “Gambiarra”)
41. defer / panic / recover
•
Defer is compared to “ensure” in ruby
•
Panic / recover are similar to exception, but
only should be used for internal APIs, never
for external APIs.
•
Panic is used for runtime errors, like array out
of bound
42. func EchoFunction(connection net.Conn, ss
*command_parser.ServerStorage) {
defer connection.Close()
!
buf := make([]byte, 1024)
n, err := connection.Read(buf)
// Heavy logic
}
43. func main() {
PanicFunction()
fmt.Println("This will not be printed")
}
!
func PanicFunction() {
defer func() {
fmt.Println("Called defer function")
}()
panic("PANIC PANIC PANIC")
fmt.Println("This will not be printed")
}
44. func main() {
PanicFunction()
fmt.Println("This will be printed")
}
!
func PanicFunction() {
defer func() {
if e := recover(); e != nil {
fmt.Printf("Recovered from %s n", e)
}
}()
panic("PANIC PANIC PANIC")
fmt.Println("This will not be printed")
}
47. require 'net/http'
!
content = []
!
thread = Thread.new do
uri = URI("http://triremi.com/")
content << Net::HTTP.get(uri)
end
!
thread.join
48. Concurrency - Go
•
Goroutines!
•
Lightweight thread implementation
•
Communications between goroutines using
channels
•
Managed by the Go Scheduler
•
Mapped to a few different OS processes
49. for {
connection, err := listener.Accept()
!
if err != nil {
fmt.Println("Error accepting the
socket")
os.Exit(2)
}
!
EchoFunction(connection, ss)
}
50. for {
connection, err := listener.Accept()
!
if err != nil {
fmt.Println("Error accepting the
socket")
os.Exit(2)
}
!
go EchoFunction(connection, ss)
}
52. // Make the channel with the number of
connections
channels := make(chan ChannelResult,
number_of_connections)
!
// Start the request in a new goroutine
go makeRequest(address, start_byte, end_byte,
out, channels)
!
//Wait for the result of the goroutine in the
channel
for(loop_var < number_of_connections) {
chan_res := <-channels
// Process the result and save to the file
}
53. Testing
•
Ruby has a built in framework (Test::Unit) and
a lot of alternatives
•
Go also has a default framework for testing,
and some early-stages alternatives
54. Testing in Ruby
•
Lots of frameworks: Test::Unit, rspec, MiniTest,
Bacon…
•
Frameworks with lots of assertions and predefined macros
•
Easy to describe behavior (BDD)
•
Everyone tests.. (RIGHT?)
56. Testing in Go
•
Light framework with the language, with a few
new options
•
Don’t come with assertions / macros
•
Tests have the same package as the
application code
•
Tests stay in the same directory of the
application code
57. package command_parser;
!
import ( “testing" )
!
func Test_parseSetCommand (t *testing.T) {
ss := new(ServerStorage)
!
str := ss.ParseCommand("SET thiagopradi
valor")
!
if ss.Dict["thiagopradi"] != "valor" {
t.Error("failed to set thiagopradi key")
}
}
}
60. Open Source
•
https://github.com/youtube/vitess - Process
and tools for scaling MySQL
•
https://github.com/dotcloud/docker - Open
Source application container engine
•
https://github.com/burke/zeus - Rails
preloader