Go & Java

Paddy Foran

Software Engineer, DramaFever

@paddyforan // paddy.io

talks.paddy.io/phillyjug

Let’s Talk About Tribalism

j.mp/devtribal

What are we doing?

Let’s Break This Down

Part 1

What’s Go?

Part 2

Why is Go gonna be weird?

Part 3

Why is Go gonna be great?

Part 4

Why is Go gonna be hard?

Part 1

What’s Go?

Go is C…

…if C were invented today.

(…and C had unicorns and magic…)

Manifests are baad.

Code should be self-documenting!

Clarity is King

Go code does exactly what it says on the page.

Write A Little, Say A Lot

Be as concise as possible without sacrificing clarity.

Automate With Tools

Go has a regular syntax that is easy to parse.

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.)

“Go is not meant to innovate programming theory. It’s meant to innovate programming practice.”

Part 2

Why is Go gonna be weird?

Go’s just like Java

Go’s nothing like Java

We have packages, too!

package fmt

“main” is a special package

package main

We import things, too!

import com.dramafever.pan.*;

Strike that, reverse it.

package main

import "github.com/DramaFever/pan"

But we import all the things!

pan.DoTheThing()
pan.MyAwesomeConstant

We Aren’t as classy as you.

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
}

But we are more composed.

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
}

Types are backwards

(We like to think ours are just sane, but y’all probably think yours are sane, too…)

public String typeBeforeTheVar;
var varBeforeTheType string

Visibility is Lexicographic

public String thisIsTotallyPublic;
private int onlyICanSeeThis;
var ThisIsTotallyPublic string
var onlyICanSeeThis int

No, Seriously, Lexicographic

type myAwesomeType struct {
    SuperCoolAttribute string
}

func NewAwesomeType() myAwesomeType {
    return myAwesomeType{SuperCoolAttribute:"This is so cool!"}
}
fmt.Println(NewAwesomeType().SuperCoolAttribute)

Syntax Stuff

for _, x := range []string{"this", "is", "so", "cool!"} {
    fmt.Println(x)
}

Pass-By-Value

(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

Part 3

Why is Go gonna be great?

Our tools are pretty great

  • Bring your own code editor
  • go build, go test, go fmt, go get
  • race detector, test coverage, memory/cpu profilers…

Go is built for open source

Sharing and reusing code is a single command

import "github.com/DramaFever/pan"

Deployment is easy

Our runtime environment is the operating system

The stdlib HTTP server is production‑ready.

And really, really easy to build with.

func HandleRequest(w http.ResponseWriter, r http.Request)

Interfaces are implicit

No more “implements”!

Concurrency is fun

Goroutines are “threads”

Channels let you communicate between goroutines.

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)
}

Part 4

Why is Go gonna be hard?

Bye-Bye, Generics

You’ll be sorely missed.

We haven’t quite figured those out yet.

GUI?

Errrr…

Gustavo Niemeyer has been doing really cool stuff with QML.

Frameworks?

We have some really good ones, but most Gophers just use net/http directly with a couple tools on top.

Hard to Slack Off

XKCD #303: Compiling

Next Steps

  • Go Tour: tour.golang.org
  • Go By Example: gobyexample.com
  • Effective Go: golang.org/doc/effective_go.html
  • GoingGo.net: goinggo.net
  • GopherAcademy: gopheracademy.com
  • golang-nuts: groups.google.com/group/golang-nuts
  • IRC: #go-nuts

Thank You

I’m out of things to talk about.