Get Go’in a Little Quicker

Even with Finals literally around the corner I couldn’t help but play around a little with “Google’s New Programming Language” go. In case you haven’t heard about go, you can find out everything you need to know at their simple site http://golang.org/.

I’m not going to talk about what I like/dislike about the language… yet. Instead I’m going to do what everyone else has probably already done. I wrote a simple compiler/link/run shell script which takes its place in my ~/bin:

#!/bin/bash
# Author: Joseph Pecoraro
# Date: Saturday, November 14, 2009
# Description: Compile, Link, and Run a go script.

# Requires a file
if [ ! "$1" ]; then
  echo "usage: go! <filename>"; exit 1
fi

# Use the proper tools for the arch
if [ $GOARCH = "amd64" ]; then
  COMPILER=6g; LINKER=6l; EXTENSION=.6
elif [ $GOARCH = "386" ]; then
  COMPILER=8g; LINKER=8l; EXTENSION=.8
elif [ $GOARCH = "arm" ]; then
  COMPILER=5g; LINKER=5l; EXTENSION=.5
else
  echo "unsupported GOARCH: $GOARCH"; exit 2
fi

# Strip .go (if its there)
file=`echo $1 | sed -e 's/.go$//'`

# Compile, Link, and Run (break on any error)
$COMPILER "$file.go" && $LINKER -o $file "$file$EXTENSION" && ./$file

Why did I write my own? The others I had seen were inadequate, didn’t work well with tabbing (filename versus executable name), or didn’t even produce a nice executable name at all. For many, you probably won’t need to compile, link, and run a program all at once (especially in a systems language). However, I’ve found that its a great tool to help me learn the language. This is due in some part to go’s quick compilation and linking.

It is really amazing to me that all of the languages that I have gotten comfortable working with have a command line shell. JavaScript, Haskell, and Ruby all have shells where I can make a fool of myself while quickly testing syntax or libraries. Go doesn’t. Its something I really miss. This little script is a simple replacement that brings me a little closer to interactivity.

I tried to make it generic so it could work on any architecture that go currently builds on. I named the script “go!”. Let me know what scripts you’ve built to work with go.

Add a Comment

search