defer Panic

Logging Panics in Go Lang on Android

To say that we are pretty pumped about today’s release of golang 1.4 that includes official android support is an understatement.

We believe android is really going to propel go’s popularity. I mean - it’s not that it doesn’t already have killer apps - docker anyone? However, now the ecosystem has just opened up an entirely new world of software engineer that is more than willing to use it.

Huge shoutout to David Crawshaw and the rest of the go team for making this happen!

We were really interested in seeing how easy it would be to integrate deferpanic support in so we could defer our panics, log our errors and watch memory and goroutines over time.

Turns out - it’s incredibly easy.

Ready to get started building android apps with golang?

The repo you will be interested in is: https://github.com/golang/mobile

The instructions are already pretty clear but I’ll go ahead and crosspost here:

Definitely use the dockerfile provided:

go get -d golang.org/x/mobile/...
docker build -t mobile $GOPATH/src/golang.org/x/mobile

Now create a regular go project just like normal && cp over the template:

mkdir -p ~/go/src/github.com/eyberg/androidtest
cp -R ~/go/src/github.com/golang/mobile/tree/master/example/basic \
  ~/go/src/github.com/eyberg/androidtest

build:

docker run -v $GOPATH/src:/src mobile /bin/bash -c 'cd /src/github.com/eyberg/androidtest && ./make.bash'

install:

Then once you launch it you can see from logcat that we are executing.

and you can see the error that was captured and posted to your deferpanic dashboard.

BAM!

As you can see - we’ve only added the two deferpanic imports and our token. After that any errors that are instatiated will be shot up to your deferpanic dashboard.

import (
    "encoding/binary"
    "log"

    "github.com/deferpanic/deferclient/deferclient"
    "github.com/deferpanic/deferclient/errors"
    "golang.org/x/mobile/app"
    "golang.org/x/mobile/app/debug"
    "golang.org/x/mobile/event"
    "golang.org/x/mobile/f32"
    "golang.org/x/mobile/geom"
    "golang.org/x/mobile/gl"
    "golang.org/x/mobile/gl/glutil"
)

var (
    program  gl.Program
    position gl.Attrib
    offset   gl.Uniform
    color    gl.Uniform
    buf      gl.Buffer

    green    float32
    touchLoc geom.Point
)

func main() {
    deferclient.Token = "v00L0K6CdKjE4QwX5DL1iiODxovAHUfo"

    err := errors.New("warning from android!")

    if err != nil {
        log.Println(err)
    }

    app.Run(app.Callbacks{
        Draw:  draw,
        Touch: touch,
    })
}

You can get the full code here: https://github.com/eyberg/androidtest

but as we mentioned - we only added a few lines. The example on the gomobile repo is missing internet access permissions so you’ll need to add those or yank the AndroidManifest.xml from our example repo.

Vote on HN
Looking to monitor your go applications?