This document summarizes a presentation given by Luka Zakrajšek on using the Go programming language for Rubyists. Some key points:
- Go is a new programming language from Google that is statically-typed, compiled, and has built-in concurrency features like goroutines and channels.
- Go offers benefits for Rubyists like easy and cheap concurrency to take advantage of multiple CPUs, low memory overhead, and easy deployment with single binary executables.
- Popular Go web frameworks like Martini and Revel were presented that are similar to Ruby frameworks like Sinatra and Rails respectively.
- Core Go concepts like packages, imports, functions, variables, types, interfaces,
1. GO FOR RUBYISTS?
Luka Zakrajšek
CTO @ Koofr
@bancek
Slovenia Ruby User Group, January Meetup
January 29, 2015
2. ABOUT ME
FRI graduate
CTO and cofounder at Koofr
Python developer in previous life
now mostly Scala, Go and JavaScript
3. GO @ KOOFR
Koofr is white-label cloud storage solution for ISPs
backend
content server (downloads, uploads, streaming ...)
FTP server
...
desktop application
GUI application
sync
remote filesystem access
4. WHAT IS GO
new programming language from Google
low level
C-like syntax
fast compilation (for large codebases)
compiles to single binary
cross platform (Windows, Linux, Mac)
5. WHAT IS GO
statically-typed
managed memory (garbage collection)
type safety
dynamic-typing capabilities
built-in types (variable-length arrays and key-value maps)
large standard library
10. EASY AND CHEAP CONCURRENCY
easy to have dozens (or even thousands) of concurrent
operations
it will even take advantage of all the CPUs available
Ruby has threads, but there is GIL in Ruby MRI
Ruby has green threads, but will use single CPU
11. LOW MEMORY OVERHEAD
Go programs can be compiled to a few megabytes binary
memory efficient while harvesting the power of the entire
machine
12. EASY DEPLOYMENT
Go programs are compiled in a few seconds into small
executables
no dependencies in production
13. USE GO FROM RUBY
Go:
packagemain
import"fmt"
import"encoding/json"
funcmain(){
mapD:=map[string]int{"apple":5,"lettuce":7}
mapB,_:=json.Marshal(mapD)
fmt.Println(string(mapB))
}
Ruby:
JSON.parse(`gorunjson.go`)
#=>{"apple"=>5,"lettuce"=>7}
https://antoine.finkelstein.fr/go-in-ruby/
16. IMPORTS
local imports (GOPATH env. variable)
import directly from web (http, git, mercurial)
capitalized identifiers are exported
import(
"fmt"
"github.com/koofr/go-koofrclient"
)
17. FUNCTIONS
take zero or more arguments
a function can return any number of results.
types come after variable names
named return values
funcmyfunction(x,yint)(xint,yint){
tmp:=x
x=y
y=tmp
return
//returny,x
}
18. VARIABLES
var statement declares a list of variables
type comes after name
varc,python,javabool
funcmain(){
variint
fmt.Println(i,c,python,java)
}
short variable declarations
funcmain(){
variint=1
k:=3//typeinference
}
19. BASIC TYPES
bool
string
int, uint, int8, uint, int16, uint16, int32, uint32, int64, uint64,
uintptr
byte // alias for uint8
rune // alias for int32, represents a Unicode code point
float32, float64
complex64, complex128