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.

Tuesday, November 10, 2009

Setting environment variables in Mac OSX

Setting environment variables in Mac OSX is really simple (especially if you have any Unix/Linux knowledge).  Note that you can use method A or B, you shouldn't do both.  Also note that I don't go into detail about how or why you have to type certain things.  This post would be 5 times longer and more boring if I did.

A. Here is how to do it from the Terminal using echo statements (easiest)
  1. Click on the Spotlight icon in the far top right of your screen.
  2. Type Terminal and press Enter.
  3. Type echo KEY=VALUE >> .profile and press Enter. *
* If the VALUE part contains a space, you must enclose it in double quotes, but you must put a foward slash in front of each double quote (i.e. echo TEST=\"blah blah blah\" >> .profile).


B. Here is how to do it from the Terminal using the vi program (still easy)
  1. Click on the Spotlight icon in the far top right of your screen.
  2. Type Terminal and press Enter.
  3. Once inside Terminal, type vi .profile and press Enter.
  4. Press the i key once to go into INSERT mode.
  5. Type or copy/paste your export KEY=VALUE combinations. *
  6. Once you are finished, press Escape.
  7. Type :wq and press Enter (yes, that is a colon).
* Only enter one KEY=VALUE combination per line.  If the VALUE part contains a space, you must enclose it in double quotes (i.e. export TEST="blah blah blah").


To verify your new environment variable(s):
  1. Close Terminal and reopen it.
  2. Type echo $KEY and press Enter.

Here are the contents of my .profile file:
export CLICOLOR=1
export GOROOT=$HOME/go
export GOOS=darwin
export GOARCH=386
export GOBIN=$HOME/go/bin
export PATH=/opt/local/bin:/opt/local/sbin:$PATH:$GOBIN

The "export CLICOLOR=1" line makes directory listings (ls command) show directories and files in different colors like Linux does by default.


The following part is specific to setting Google Go environment variables.

The four lines that begin with "export GO..." are the four environment variables needed to install and use the new Google Go programming language.  Please note that you need to create the directories referred to in the $GOROOT and $GOBIN variables.  You also need to include the $GOBIN path in your $PATH variable. You do that by appending $GOBIN to the PATH=... line, but you must do this after you declare the $GOBIN environment variable.


The following command will create that directory.
mkdir $GOROOT

mkdir $GOBIN

To verify your Google Go variables, use this command:
env | grep GO


I hope this post has been a help to at least one person.  Comments, corrections, and suggestions are welcome.  Good luck!

Thursday, October 1, 2009

Automatically Empty Trash When Ejecting Removable Drive (Mac)

Problem:  When I “delete” pictures from an SD card on a MacBook without emptying the Trash, the SD card is still full when I insert it back into the digital camera.  Although not nearly as annoying, the same thing happens when I forget to empty the Trash before ejecting a USB drive.

Solution:  There may be a much better way of doing this, but I searched Google for a better solution.  So I wrote this little bash script that would delete the .Trashes folder and other things Mac OS X tries to hide on there.  The bash script below will intercept the command to eject a volume, do some checking on it, empty the trash on that drive, then pass control to the real eject command.  The main "trick" is to make sure the drive is formatted as MSDOS, which will be any removable drive you are using in both Mac OS X and Windows. 
You will need open Terminal, rename /sbin/umount to /sbin/umount2, then copy the contents below into a file and save it as /sbin/umount.  This has been tested in Leopard and Snow Leopard.

Disclaimer:  I am not responsible if you mess something up.  Perform these steps at your own risk.  I intentionally didn't explain step by step how to make this work.  If you have no idea what the sentence in red means, then you should not attempt this hack. If you are an old school Linux/Unix user or familiar with the Terminal app then you should have no problem getting this to work.


#!/bin/bash

if [ $# -gt 0 ]
then
  vol=`df -T msdos | grep "$1"`

  if [ "$vol" != "" ]
  then
    nlen=`expr ${#1} - ${#1} - ${#1}`

    if [ "${vol:(nlen)}" == "$1" ]
    then
      /bin/rm -rf "$1/.Trashes"
      /bin/rm -f  "$1/._.Trashes"
      /bin/rm -rf "$1/.fseventsd"
      /bin/rm -rf "$1/.Spotlight-V100"
    fi
  fi
fi

/sbin/umount2 "$1"