http://gerg.ca/talks/go-pycon/
var a int = 42
var b int = 24
// := is assignment with type inferrence
c := a + b
- Slices are like lists
- First order functions
- Interface types (abstract classes work like coffeescript, but checked at compile time)
- Attach methods from any user-defined types
- No inheritance, only composition/delegation
- Interfaces
Instead of delegating to a composed type, like this:
type User struct {
foo Foo
}
function (u User) DoThingOnFoo() {
return Foo.bar()
}
You can do this!
type User struct {
Foo // don't assign a variable name to the type
}
main (u User) {
u.bar() // automatic delegation
}
Interfaces:
type PrivilegeSet interface {
HasPrivilege(name string) bool
SetPrivilege(name string)
}
Go doesn't have exceptions.
- Return multiple values instead, follow a convention.
- Use
defer
instead oftry/finally
. - You can use
panic
for a super error, like no more memory. - Controversial design decision...
- Called
goroutines
. - They communicate over channels. Channels define a certain type.
- DONT COMMUNICATE BY SHARING MEMORY, INSTEAD SHARE MEMORY BY COMMUNICATING.
func main() {
// launch a background goroutine, send an
// endless stream of requests to it, and
// print each responses
data := make(chan string)
go responder(data)
for {
data <- "ping"
response := <-data
fmt.Printf("<- %s\n", response)
}
}
// receive a stream of requests and send appropriate responses
func responder(data chan string) {
for {
request := <-data
fmt.Printf("%s ", request)
switch request {
case "ping":
data <- "pong"
default:
data <- "huh?"
}
}
}
- New features are shot down on mailing lists
- If the language doesn't support it, just can build it
- Use data instead of code to do stuff (use structs, tuples, etc)
- Not as fun/easy/productive as Python
- Infinitely better C, Java or C++.
- "C and C++ don't need to exist anymore"