Becoming a GoPHPer

Paddy Foran

Software Engineer, DramaFever

@paddyforan // paddy.io

talks.paddy.io/gophper

I hate PHP

… is what you expect me to say.

Let’s Talk About Tribalism

j.mp/devtribal

What are we doing?

Please be nice

My PHP skillz are not leet.

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

(…and C had decades of programming languages to pilfer shamelessly from…)

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 bit.ly/gotools1 and bit.ly/gotools2

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

Part 2

Why is Go gonna be weird?

Go is Compiled

But veeeeeery quickly.

Go is not a web language

The request is no longer your only or default context.

Go has packages

Files don’t matter, packages do.

package fmt

“main” is a special package

package main

Packages are imported

package main

import "github.com/DramaFever/pan"

We Aren’t as classy as you.

class Everything
{
    public $status = 'awesome!';

    public function getStatus() {
        echo 'everything is '.$this->status;
    }
}
type Everything struct {
    status string
}

func (e Everything) GetStatus() {
    fmt.Printf("Everything is %s", e.status)
}

But we are more composed.

type Lego struct {
    Length int
    Width  int
}
type Person struct {
    Name string
}
type LegoPerson struct {
    Lego
    Person
}

benny := new(LegoPerson)
benny.Length = 1
benny.Width = 2
benny.Name = "Benny"

Types are static

var thisWillAlwaysBeAString string

Initialization is not assignment

var masterBuilder bool
masterBuilder = true
batmansFavouriteColour := "black"
batmansFavouriteColour = "black and sometimes gray"

Scope is much more a thing

i := 0
for i := 0; i < 3; i++ {
    fmt.Println(i+1)
}
fmt.Printf("outside for loop: %d", i)
1
2
3
outside for loop: 0

Visibility is Lexicographic

class Kragle
{
    public $public = 'Public';
    protected $protected = 'Protected';
    private $private = 'Private';
}
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 amazing

type ThingDoer interface{
    DoTheThing() bool
}

type ThingDoerImplementation struct{}

func (t ThingDoerImplementation) DoTheThing() bool {
    fmt.Println("Did the thing!")
    return true
}

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) {
    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://tek.phparch.com"}

for _, u := range urls {
    wg.Add(1)
    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?

Static Typing

Just a new way of thinking.

Frameworks?

We have some really good ones, but most[citation needed] 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

Shameless Plug

DramaFever is hiring. Frontend, backend, whatever. Come find me, or paddy@dramafever.com

Thank You

I’m out of things to talk about.