Member Avatar for BobTheLob

Hey guys, it's been quite a while since i've been on here :P
I'm fiddling around with the Go language. However, I'd much rather have a build script that'll compile the program for me than typing in the lines of code necessary every time:

6g hello.go
6l hello.6
./6.out

Even though that is not much, I would also like to learn some more about scripting, so i figure this would be as good a time as any.
I can easily create a script file that will execute that exact code, and it works. But what I want to do is make a script (I'm not even sure if it's possible or what the best approach is) file that will be able to know (by somehow me telling it what it is to compile) what file I want to compile and run, and then do the code above for it.
How would I go about doing this?
PS. I'm running Mac OS X and want to do this via Terminal (Unix based stuff)

Recommended Answers

All 3 Replies

You want to use a shell variable and basename function. Something like this file

#!/bin/bash
while [ $# -gt 0 ] ; do
  case $1 in
  -h*|--h*)cat<<EOMSG
Usage: $(basename $0) file_to_compile
  Compiles and runs a go program found in a single file
EOMSG
     exit 0;;
  *) file=$1; shift ;;
  esac
done
if [ -z "$file" ] ; then
  echo "You must specify a file to compile"
  exit 1
fi
case $file in
  *.go) echo "OK with file $file";;
  *) echo "File must be named _something_.go"; exit 1;;
esac
base=$(basename $file)
echo "Compiling $file"
6g $file
echo "Linking $base.6"
6l $base.6
echo "Running 6.out"
./6.out
echo "Done running"

Of course you can make it significantly more complex by adding options. For instance, passing command line arguments to the compiler or linker.

I would probably do all this using make or some other build tool; but this little shell script is a decent start

Member Avatar for BobTheLob

Sweet man. I'm still learning about shell scripting, so i'll take a detailed look at what all that is :P
Now make i have not used before. Is there a decent tutorial around where i can learn about that?

Make stuff:
Gnu docs This is the full monty

Beware that in makefiles, the leading <tab> character is syntactically significant and visually indistinguishable from several spaces is a major cause of newbie difficulties: Makefile rules have leading <tab>s NOT leading spaces

In the introduction to the go programming language I found this:

To build more complicated programs, you will probably want to use a Makefile. There are examples in places like go/src/cmd/godoc/Makefile and go/src/pkg/*/Makefile. The document about contributing to the Go project gives more detail about the process of building and testing Go programs.

Since you have the go compiler running, you should have those Makefiles available to you as examples.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.