On Github victorcoder / por-que-go
Fun, efficient and open source
Statically typed and compiled, but it feels lightweight and dynamic
Designed for modern computing (multicore)
Aimed at software such as webservers, but turned out to be a great general-purpose language
//Serving http://localhost:8080/world: package main import ( "fmt" "net/http" "log" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, %q", r.URL.Path[1:]) }) log.Fatal(http.ListenAndServe(":8080", nil)) }
Multiple return values
func foo() int, string { return 1, "OK" }
Named return parameters
func foo() (bar int){ bar = 2+2 return }
func Greeting(prefix string, who ...string) Greeting("hello:", "Joe", "Anna", "Eileen")who, will have the value
[]string{"Joe", "Anna", "Eileen"}
func foo(f func()) { x := f() func() { println(x) }() }
foo := "This is a string type"
type Foo interface { privateMethod() int PublicMethod() int }
Start a goroutine
go f()
Channel sends
ch <- value
Channel receive
value = <-ch
Starting a goroutine
func (s *Service) Start() chan request { ch := make(chan request) go s.serve(ch) // s.serve runs concurrently return ch // returns immediately }
Dependencies are expressed in source code
import "github.com/bmizerany/pat"
Two purposes, import path and library repository
Just push your code to a public repository
The "go get" command download and install all rependencies
Easy to define, easy to use
Emphasis on simplicity
Garbage collected
Memory layout control (pointers but no pointer arithmetic)
Consistent standard library
Integrated testing
Ultra fast compiler (can be used as scripting language)
Target i386, amd64, arm (Mobile development)
Go 1 released 28/03/2012
Easy installation: Binaries for OSX, Linux, Windows
Stable, mature and suitable for real world problems
Used in production at big companies
Very active community and growing fast
Erlang and Scala implements the Actor model, Go implements goroutines
Scala has thread based Actors that are heavyweight
goroutines are lightweight, threads are heavy
All three models uses channels for messages
In Scala passing around primitive data like integers is expensive because of the JVM legacy
node.js/JS/V8 doesn't have concurrency features but comparable for server programming
Awesome standard lib but not enought
Go stdlib, http, templates, websockets, tar/zip, json, xml, sql, smtp, etc...
node code is by definition non-blocking and asynchronous
Go let's you write synchronous looking code which behaves like if it were asynchronous, without requiring you to handle the callback spaghetti
Many libs in Go stdlib are non-blocking
Fibonacci sequence made by the infamous post http://teddziuba.com/2011/10/node-js-is-cancer.html
node.js
var http = require('http'); function fibonacci(n) { if (n < 2) return 1; else return fibonacci(n-2) + fibonacci(n-1); } http.createServer(function (req, res) { res.writeHead(200, {'Content-Type' : 'text/plain'}); res.end(fibonacci(40) + ''); }).listen(1337);
Go
package main import ( "fmt" "net/http" "log" ) func fibonacci(n int) (result int) { if n < 2 { result = 1 } else { result = fibonacci(n-2) + fibonacci(n-1) } return } func main() { http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { w.Write([]byte(fmt.Sprintf("%d", fibonacci(40)))) }) log.Fatal(http.ListenAndServe(":8080", nil)) }
node.js 0.10.16
time curl http://localhost:1337 165580141 real 0m2.097s user 0m0.007s sys 0m0.005s
Go 1.2
time curl http://localhost:8080 165580141 real 0m1.323s user 0m0.007s sys 0m0.005s