2009年11月14日土曜日

Tried Go Language on Snow Leopard

On my Macbook (Mac OS X 10.6) I installed the Go language.

Basically I just followed the instruction after I watched the video on the website:

http://golang.org/

Added the following environment variables to my .bashrc:
export GOROOT=$HOME/go
export GOARCH=386
export GOOS=darwin

Then sourced the .bashrc (just this time because I did't want to restart my shell):
$ . ~/.bashrc


Installed mercurial($ is my prompt):
$ sudo easy_install mercurial

I've already had python 2.5.4 installed.

Then I tried to checkout source code from the Go language reposository:
hg clone -r release https://go.googlecode.com/hg/ $GOROOT

But then I got some error which seemed to come from my locale setup. For now I added the following to my .bashrc (not sure if that's the best workaround but anyway...):
export LC_CTYPE=C

Sourced .bashrc again, then retried the hg command to checkout the code.
Now it worked!

I've already had the latest XCode installed on my Macbook, so I already have required tools to build Go environment. So I just typed did as follows to build the Go system:

$ cd $GOROOT/src
$ ./all.bash

Then after a few minutes I saw this after lines of build commands:

--- cd ../test
0 known bugs; 0 unexpected bugs

Which means Go's ready here!

So I write my first Go program hello.go:

package main

import "fmt"

func main() {
fmt.Printf("hello, 世界\n")
fmt.Printf("123456789 * 123456789 = %d\n", 123456789 * 123456789)
}


Attempted to compile:
$ 8g hello

Which tells me:
c0 a4
b3 a6
hello.go:6: illegal rune in string
hello.go:6: illegal rune in string
hello.go:7: syntax error near fmt

Hmmm, multibyte string seems to cause some errors. So for now I change them to simpler "world" for now...:

fmt.Printf("hello, world")

What about the syntax error thing at line 7? By taking a look at the tutorial, I could see that I probably need a semicolon at the end of the line if I put more than one statement in a function (might not be correct to say so... oh, now I found there's some description on semicolons in "Effective Go" page). Anyway if I put at the end of line 6, the syntax error at 7 is gone, but instead this error comes along:

hello.go:7: constant 15241578750190521 overflows int

Sure, it's more than int can handle... So I look for some piece of info on variable types... OK, so there's 64-bit int (described at Printing section of Effective Go at least, and feels like there must be somewhere else too). Now I modify my source code at line 7:

fmt.Printf("123456789 * 123456789 = %d\n", int64(123456789 * 123456789))

Then my source code compiles successfully!
$ 8g hello.go
$ 8l hello.8

So I run the linked binary (unless you specify with -o option your target will be named "8.out" with the 8l command):

$ ./8.out

hello, world
123456789 * 123456789 = 15241578750190521


Examined the result with the bc command (a calculator command usually installed on *nix based systems), and looks right.

So my first Go code worked (except for the multibyte string issue) and I can start playing with the language.

0 件のコメント: