Loading
2 comments

Going Go on Windows

 

I admit it: I love all things Google. Gmail, Google+, Google Music, Google Docs, AppEngine, Apps for Domains, AdSense, Analytics -- I use and enjoy them all. I was very excited when I first saw the announcement about Google’s new programming language Go, but I didn’t set about learning it right away. I was deterred not having any particular project build with it, and it was not available for Windows, my platform of choice.

The first problem was solved by the addition of the Go runtime to the AppEngine platform. It was a perfect fit for a project that I had been working on with the idea of deploying it on node.js. I still think that I might build a backend for this program in Node, not only to have a backup to AppEngine but also to learn Node really well. The second problem has mostly been solved by the release of the official SDK for Windows. After downloading and installing it, I was immediately about to compile and link, but using other tools like gomake and gotest was not working. Through a great deal of trial-and-error, I eventually settled on a solution that now works very nicely for me.

The biggest hurdle to overcome is that many of the Go tools assume the existence of the standard UNIX developer toolchain, such as make. There are many packages that offer ports for UNIX: Cygwin, GnuWin32, Djgpp, Microsoft’s Windows Services for UNIX and MinGW just off the top of my head. I tried all of them and eventually settled on the MSYS package from MinGW. While it doesn’t contain an entire development environment, it has bash, make and a terminal window (rxvt) that is a vast improvement over the stock DOS command window.

After installing MSYS, you want to make some config changes to make things easier for you. In etc/fstab, you can map your long and complicated Windows paths to directories in your MSYS home directory. For instance, I like to map C:/Users/adam/Documents/Projects to /home/Adam/projects, so when I start up the MSYS shell, I can just cd projects and I will be were all my code lives. Also, you may want to modify the .bash_profile in your MSYS home directory such that it creates and exports the Go-related environment variables that will make the Go tools work correctly.

Once you have all that set up, you should be able to start up the MSYS terminal, cd to the directory where your Go project lives and use gomake and gotest to compile, link, install and run unit tests.

I have been having a great time learning Go. It is a wonderful language, and I really hope that it gains traction. I will be having a lot more blog posts in the near future about it and some of the work that I am doing with it. I already have an open source Go project hosted on bitbucket: mtemplate.
0 comments

AppEngine/Go Knowledge Resources

I have been spending a great many evenings working on a web application that will -- some day -- run on AppEngine's Go runtime. Go is a wonderful language, and programming with it is fun in a way that is increasingly uncommon. Still, it is a very new language and good references are rare. Useful documentation for the AppEngine/Go platform is even hard to come by; the so-called "Reference" for the Go interface to the datastore is little more than a few simple examples and a trivial list of API calls. It's disappointing, frustrating and unproductive.

Fortunately, the web is full of smart people who are willing to go out of their ways to share what they have learned, and I am gradually discovering all of them. I will be listing them here for my own reference as well as making the discovery process less difficult for my fellows.

Ugorji Nwoke's Blog is full of great ideas and thoughts about AppEngine/Go and AppEngine as a platform generally. Very smart, interesting guy.

Miek Gieben's Learning Go isn't specifically about AppEngine, but it is an invaluable reference for the GO language.

GoLang Tutorials has some very valuable guides for obtaining a better mastery of Go. If you find yourself feeling a bit confused by something that Go does a little differently than other programming languages (like interfaces) you may well find a well-written and informative post here that will clear the way for you.

 

So, it is a meager collection at the moment, but I will be adding to it every chance that I get. Also, I hope to be making my own blog a useful resource, as I have a couple of good techniques and ideas that I have come up with while working on my project.

0 comments

A custom iterator pattern for Go

I have been working on a substantial web application that will be deployed on Google AppEngine’s Go runtime. Learning and programming in Go has been a very rewarding experience. It is a terrific language. I feel enormously productive with it, and it’s easy to keep the whole thing in my head at one time. It is not without its foibles and hills to climb, of course. All languages have quirks and intentional design decisions that one runs up against.


In my case, I found myself needing to create a custom iterator for a type that I had created. My custom type Foo was commonly found in collections, and it is easy enough in Go to make a slice of any type: []Foo. I was able to iterate over the collections easily with Go’s range. I was all set.


Before long, I found myself needing to add a function that would take my []Foo as a receiver, but Go does not allow me to have a slice of anything as the receiver in a function definition:


func (this []Foo) Gronk() int {} // not allowed!


I needed to declare a type for my slice:


type FooList []Foo


I can now declare my method:


func (this FooList) Gronk() int {}


But now that the slice of Foo is its own type, I can no longer use it with range:


// where fooVals is a FooList popualted with values...
for idx, val := range fooVals {} // not allowed!


Unfortunately, Go does not provide a mechanism that allows me to make my custom type compatible with range. In C#, I can have any class implement the interface IEnumerable, and it will work for for...in or any other iteration mechanism. I’ve always been a little uncomfortable with the idea that a basic language mechanism is tied to an interface that is imported from a DLL, but it is very pragmatic and works well. I wouldn’t hold my breath for Go to do something similar, so I knew that I’d have to find an alternative or roll my own iteration mechanism.


One alternative that I explored is turning FooList into a struct and exposing my slice as a public member:


type FooList struct {
    Items []Foo
}


With that, I could use range to iterate over fooListInstance.Items, but I didn’t really want to make the slice public. It’s probably a bit of an artificial restraint, as I am not writing the code to be part of a redistributable library, and I can probably trust myself not to abuse the compromise of encapsulation, but I decided to stand on principle and find a better solution.

I tried adding methods that would allow me to iterate over the items while keeping the slice private:

  • Reset() // Move to the beginning of the collection
  • HasNext() // is there another item available? and
  • Next() // Get the next item in the collection.

That worked rather nicely until I found myself needing to use the collection with two simultaneous and different iteration points. I followed several different lines of inquiry and experimentation before settling on what I think is a very workable solution.


One of Go’s greatest features is its support for closures, first-class functions and higher-order functions. A good programmer can make a lot of beautiful magic happen with those capabilities, and they provided me with the answer that I needed.


Given a FooList definition of

type FooList struct {
    items []Foo
}


I add the following type and method:


type FooListIterator func() (Foo, bool)
// Calling the FooListIterator function returns a Foo and a
// bool that tells me if the returned value is valid. If it is false,
// I have iterated off the end of the list and the Foo has some
// useless default value.
func (this FooList) GetIterator() FooListIterator {
    return func(list []Foo) FooListIterator {
        index := 0
        return func() (Foo, bool) {
            if index >= 0 && index < len(list) {
                val := list[index]
                index++
                return val, true
            }
            return 0 ,false
        }
    }(this.items)
}


In my code, I use it like this:


func DoSomething(someFoos FooList) {
    iter := someFoos.GetIterator()
    val, good := iter()
    for good {
        // Do something useful with val
        val, good = iter()
    }
}

Here is a complete example that you can C&P into the Go Playground. You can also view it directly in the Playground.

It’s not as neat and simple as range, but it seems to me that this approach is reasonably straightforward and pragmatic as well as flexible. I’m using it now in my large codebase, and I hope that it may provide some utility to my fellow Gophers.

If you have any suggestions or criticism about this code -- or if you find it useful in your own projects -- I would very much like to hear about it. Please consider leaving a comment.

 

UPDATE:

While nosing around on the golang-nuts Google Group, I came across a post that asked the same question that I did, and lo-and-behold, Steve Blenkinsop came up with the exact same solution that I did. You can see his working example in the Go Playground. I think that it is wonderful that someone else had the same idea that I did, as it affirms my belief in the usefulness of the approach.