Being too clever merging protobufs

This is probably something lots of other people have tried and burnt themselves with, but anyway, this time it’s my turn. The goal is, given an arbitrary protobuf, can we write an API that applies default values to it? Normally we would create a prototype protobuf object with the defaults and merge our current object into it, updating the prototype object with current values. However, in Go, this would erase the type information and mean we would have to do some ugly casting (it’s easy to avoid this in C++ by using templates).
Read more...

Setting the HTTP User Agent in Go

Here’s the smallest amount of code I could come up with to set the user agent when making a HTTP request in Go:

type UserAgent string

func (u UserAgent) RoundTrip(r *http.Request) (*http.Response, error) {
    r.Header.Set("User-Agent", string(u))
    return http.DefaultTransport.RoundTrip(r)
}

http.DefaultClient.Transport = UserAgent("my-great-program")

Note that this isn’t really legal, since the RoundTripper is not supposed to modify the request.

Read more...

Updating go_resources in Homebrew

This is a quick note to myself about how to update the go_resources in a Homebrew formula. First, install godep and the Homebrew dev tools: $ cd $GOPATH $ go get -u github.com/tools/godep $ brew tap homebrew/dev-tools Next, generate a Godeps file in your Go project $ cd $GOPATH/src/github.com/me/my-project $ $GOPATH/bin/godep save . Now you can get brew to generated the go_resources that you can just paste into your formula:
Read more...