golang: invalid operation: someVar == “” (mismatched types *string and string)

I was trying to see if a value was empty in a golang script I was writing, when I came across an odd error message:

invalid operation: someVar == "" (mismatched types *string and string)

It turns out that the following code doesn’t return a simple string, but an instance of flag.String

someVar := flag.String("somevar", "", "this is a random flag")

To perform the comparison, you need to use a pointer to it as follows:

if *someVar != "" {
    // Do this

Working with Memcached via CLI

A lot of the time when I’m working on a project that uses memcached, I’ll want to have a look at the data in the cache without using my application to make sure that it’s doing what I expect. I didn’t expect that wanting to list all keys on a memcached server was such a big request, but it looked like there wasn’t any easy way to do it.

I did start a project named mempp to provide a CLI friendly interface to memcached via the Python bindings, but as it turns out you don’t even need this. libmemcached ships with everything you need.

First, you’ll need to install libmemcached-tools

sudo apt-get install libmemcached-tools

Then, if you want to list all keys stored on a server, you can use memcdump

$ memcdump --servers localhost:11211

If you want to see the values of those keys, use memccat

$ memccat --servers localhost:11211 key_one

If you want to remove a key, use memcrm

$ memcrm --servers localhost:11211 key_one

With a combination of memcdump and memccat, plus memcrm you can poke around your memcached instance without needing to use telnet or list slabs or anything like that, which most posts advise you to do.

Installing godoc for golang (go 1.2)

I recently read about godoc, but when I went to try it the binary wasn’t installed.

Fortunately, it was a relatively easy fix:

go get code.google.com/p/go.tools/cmd/godoc

As it wants to write to a privileged location (/usr/lib/go/bin/), you might need to run it as root

sudo -E go get code.google.com/p/go.tools/cmd/godoc

The -E flag means “preserve environment variables”, which you’ll need to preserve your $GOPATH.

Next, add /usr/lib/go/bin/ to your path:

export PATH=/usr/lib/go/bin/:$PATH

Then run godoc:

$ godoc fmt Println
func Println(a ...interface{}) (n int, err error)
    Println formats using the default formats for its operands and writes to
    standard output. Spaces are always added between operands and a newline
    is appended. It returns the number of bytes written and any write error