Wednesday, November 11, 2009

Link and compile .go files with one command

So, if you're like me and like to type as least as possible when editing and recompiling programs over and over, then this might be a bit of help.  This tutorial assumes you are familiar with using the Terminal program or equivalent Linux console.  If not, then you probably haven't installed the Google Go language yet anyway.

Here is how to do it from the console.
  1. Type vi $GOBIN/go and press Enter. *
  2. Press the i key once to go into INSERT mode.
  3. Copy the two lines of code below and paste it into the new file.
  4. Once you are finished, press Escape.
  5. Type :wq and press Enter (yes, that is a colon).
  6. Type chmod 755 $GOBIN/go and press Enter.
* This assumes you have declared $GOBIN environment variable.  See previous post if you have not.

Copy the two lines below and paste into new file:
#! /bin/bash
8g "$1" && 8l -o "${1/.go/.out}" "${1/.go/.8}" && rm -f "${1/.go/.8}" && echo "Output: ${1/.go/.out}"

Note that I am using 8g and 8l commands because I am using a Mac.  If you are using a different OS, then refer to this page and replace all the 8's with the appropriate number.

What It Does:
  1. Compiles the file.go source file into a file.8 object file.
  2. Links the file.8 object file and creates a file.out executable program.
  3. Deletes the file.8 object file.
  4. Outputs a confirmation message.

Usage and Output Demonstration:
jray$ ls
hello.go
jray$ go hello.go
Output: hello.out
jray$ ls
hello.go  hello.out
jray$ ./hello.out
Hello World!

Note that the output file is named hello.out instead of the default 8.out.

No comments:

Post a Comment