Robo

Scripting the Go-compiler

Posted in Messing around, Shell Scripts by Robin Ramael on November 17, 2009

A good programmer is a lazy one, the cliche says. Maybe. An easily distracted one if nothing else. While trying out Google’s newly released compiled language Go, it bothered me that there was not a faster way to compile, link and execute the little programs I was writing. So, rather than to teach myself something actually useful i wrote a script to do this all at once.

It is invoked by goc (best move it to /usr/bin first), the file to compile and then the arguments to give the program when it executes.

The -s flag will try and mute all compile and linking errors and the -m flag will rename the executable to whatever your .go file’s name was.

You can download it here. Sourcecode after the jump.


#!/bin/bash
# goc
# by Robin Ramael, no rights reserved whatsoever.
# compiles, links and executes small go-programs
# for larger programs use makefiles
# usage: goc [-s silent: tries not to output errors, only the compiled program's output]
#	 [-m rename the output program to the name of the .go file, without this flag the binary will be called 6.out]
#	 <file to compile; link and execute>
#	 <arguments to give to the program on output>
#argument mumbo-jumbo
silent=
move=
while getopts 'sm:' OPTION
do
case $OPTION in
s) silent=1
;;
m) move=1
;;
?) printf "usage: goc [-s] [-m] file"
esac
done
if [[ $silent ]]; then
shift 1
fi
if [[ $move ]]; then
shift 1
fi
tocomp=$1
shift 1
#$* are now the arguments we want to give our program
#/argument mumbo-jumbo
#clean up so we avoid weird errors
rm -f  6.out
# if the -s argument is given, don't output error
if [[ "$silent" ]]; then
6g $tocomp >> /dev/null
echo $tocomp | sed 's/\.go/\.6/' | xargs 6l >> /dev/null
else
6g $tocomp
echo $tocomp | sed 's/\.go/\.6/' | xargs 6l
fi
if [[ -e 6.out ]]; then
./6.out $*
if [[ "$move" ]]; then
cp 6.out `echo $tocomp | sed 's/\.go//'`
fi
fi
#clean up
echo $tocomp | sed 's/\.go/\.6/' | xargs rm
rm 6.out

Note that the go compiler will always overwrite or remove the 6.out file in the current directory, even if the -m flag is raised.

Advertisement

One Response

Subscribe to comments with RSS.

  1. Arno said, on December 10, 2009 at 11:04 am

    Rather THAN.
    i -> I


Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.