@paddyforan // paddy.io
talks.paddy.io/phillyjug
j.mp/devtribal
Code should be self-documenting!
Go code does exactly what it says on the page.
Be as concise as possible without sacrificing clarity.
See http://talks.golang.org/2012/splash.article#TOC_17. and http://blog.golang.org/cover#TOC_2.
(Yes, the periods at the end are part of the URLs.)
package fmt
“main” is a special package
package main
import com.dramafever.pan.*;
Strike that, reverse it.
package main
import "github.com/DramaFever/pan"
pan.DoTheThing()
pan.MyAwesomeConstant
public class Dog{
String breed;
int age;
void bark(){
// do the thing
}
}
type Dog struct {
breed string
age int
}
func (d *Dog) Bark() {
// do the thing
}
type Animal struct {
Genus string
Species string
Calories int
}
func (a *Animal) Eat(f Food) {
a.Calories += f.Calories
}
type Dog struct {
Breed string
*Animal
}
(We like to think ours are just sane, but y’all probably think yours are sane, too…)
public String typeBeforeTheVar;
var varBeforeTheType string
public String thisIsTotallyPublic;
private int onlyICanSeeThis;
var ThisIsTotallyPublic string
var onlyICanSeeThis int
type myAwesomeType struct {
SuperCoolAttribute string
}
func NewAwesomeType() myAwesomeType {
return myAwesomeType{SuperCoolAttribute:"This is so cool!"}
}
fmt.Println(NewAwesomeType().SuperCoolAttribute)
for _, x := range []string{"this", "is", "so", "cool!"} {
fmt.Println(x)
}
(Even pointers)
type Gophers struct {
Count int
}
func Convert(g Gophers) {
g.Count++
}
gophers := Gophers{}
Convert(gophers)
fmt.Println(gophers.Count)
Convert(gophers)
fmt.Println(gophers.Count)
Zeroes all the way down
type Gophers struct {
Count int
}
func Convert(g *Gophers) {
g.Count++
}
gophers := Gophers{}
Convert(&gophers)
fmt.Println(gophers.Count) // 1
Convert(&gophers)
fmt.Println(gophers.Count) // 2
Sharing and reusing code is a single command
import "github.com/DramaFever/pan"
func HandleRequest(w http.ResponseWriter, r http.Request)
You can think of channels as really lightweight queues.
func Get(url string, resp chan *http.Response, wg *sync.WaitGroup) {
wg.Add(1)
defer wg.Done()
r, _ := http.Get(url)
resp <- r
}
func WaitAndClose(wg *sync.WaitGroup, resp chan *http.Response) {
wg.Wait()
close(resp)
}
resp := make(chan *http.Response)
wg := new(sync.WaitGroup)
urls := []string{"http://google.com", "http://dramafever.com",
"http://paddy.io", "http://phillyjug.com"}
for _, u := range urls {
go Get(u, resp, wg)
}
go WaitAndClose(wg, resp)
for r := range resp {
fmt.Println(r.Status)
}
We haven’t quite figured those out yet.
Gustavo Niemeyer has been doing really cool stuff with QML.