This guide covers the basics from installing Go programming language in your Ubuntu Linux machine to making a simple version of the famous "factorial" program and running it in your system. Other tools needed:
Command: sudo apt-get install mercurial
Command: sudo apt-get install bison gcc libc6-dev ed gawk make Let's get it started! 1. Create a folder for all your go files in your home directory. Call it go. Command: mkdir $HOME/go 2. Go depends on some environment variables for its compilation. You need to set these up:
Command: export GOROOT=$HOME/go
Command: export GOOS=linux
Command: export GOARCH=386 Note: See http://golang.org/InstallingGo/EnvironmentVariables for more valid combinations of $GOOS and $GOARCH
Command: mkdir $GOROOT/bin Then, Command: export GOBIN=$HOME/go/bin 3. Add the location of your go binaries to what's existing in the environment $PATH variable. Command: export PATH=$PATH:$GOBIN 4. Check out the Go repository. Command: hg clone -r release https://go.googlecode.com/hg/ $GOROOT 5. Build Go. Commands:
--- cd ../test and .. N known bugs; 0 unexpected bugs where N, the number of known bugs, depends on the release. 6. Update GOROOT to point to the src directory. Command: export GOROOT=$GOROOT/src That's it! Now let's have our sleeves rollin', make a program and test it out! OUR LITTLE BEGINNER PROGRAM We will call this little program factorial. We put it inside the file factorial.go //start of factorial.go package main import fmt "fmt" import flag "flag" var input *int = flag.Int("input", 5, "specify the input value; must be an integer") func main() { flag.Parse() result := 0 result = factorial(*input) fmt.Printf("The factorial of %d is %d\n", *input, result) } func factorial(n int) int { if n <= 1 {return 1} return n * factorial(n - 1) } //end Let's first dissect the codes. The notation // identifies a single-line comment. Multiple line comments use /* */ notation, just like in C and java. The line 'package main' identifies that our program is part of the main package. Every go source file must declare which package it is part of. The 'import ' declaration lets us access certain package/s to make use of their functions. Here, we import packages fmt and flag and call them "fmt" and "flag", respectively when we reference them in our program. If you reference the package just the same, you may omit its explicit name. We can just write import "fmt". If we prefer, we may also group multiple import statements. Above, we could have written: import ( "fmt" "flag" ) We import the package fmt because we make use of its Printf function. For more details about fmt package, see http://golang.org/pkg/fmt We import flag because we make use of its Int function. We let the user specify in the command line the argument to the factorial method. We make use of flag to identify the value of the input variable. For more details about the flag package, see http://golang.org/pkg/flag var var_name type = initial_value is the notation for declaring a variable and assigning it its intial value. In var input *int = flag.Int("input", 5, "specify the input value; must be an integer"), we specify that the variable input is of type pointer to an integer value (*int, Why *int? Because it is the return type of the flag.Int() function), and assign it the declaration of the int flag we use as the command-line argument in which the user can specify the integer to which our program must compute the factorial of. We call the flag input, assign it the default value of 5, and "specify the input value; must be an integer" is the help message for our flag. Functions in go are declared with the func statements. Like in C, go programs begin its execution inside the main method. Inside main we call flag.Parse() to parse the command line arguments. result := 0 is an idiom to var result int = 0. We get the value of the input variable, and have it as a parameter to our factorial method. Now, our factorial method accepts a parameter of type integer and returns a value of type integer: func factorial(n int) int{...} You might have noticed that while C and Java declares the variable type and method return type before the variable or method name, it is reverse in Go. The factorial method is actually a recursion. Recursive algorithms are not part of this tutorial. =) Notice that using fmt.Printf() is just like using the printf function in C. There are other fmt functions like Print() and Println() where you don't have to provide format specifiers for the variables anymore. In printing the result, we could have called directly factorial(*input) in the arguments list instead of making use first of the result variable. Factorial Program (REVISED) //factorial.go revised package main import ( "fmt" "flag" ) var input *int = flag.Int("input", 5, "specify the input value; must be an integer") func main() { flag.Parse() fmt.Printf("The factorial of %d is %d\n", *input, factorial(*input)) } func factorial(n int) int { if n <= 1 {return 1} return n * factorial(n - 1) } //end To compile the program, we depend on the $GOARCH variable that we declare. Since we declared it 386, we will make use of the 8g compiler and 8l linker. For more information, see http://golang.org/doc/install.html#tmp_35 Command: 8g factorial.go If all goes well, nothing is printed after this command. In the current directory, factorial.8 is created. Command: 8l factorial.8 If all goes well, nothing is printed after this command. In the current directory, an executable 8.out is created. Then we execute the program by: Command: ./8.out -input=3 If all goes well, it would print "The factorial of 3 is 6" Notice the input flag we declare in the program. It has a minus sign before it, that means it is a flag. If you just command ./8.out without the input flag, it would use the default value we specified thus would print "The factorial of 5 is 120" Note: Why use flag? For our program to easily spot the correct argument in the command line. Thus if we command something like Command: ./8.out -input=3 some text some flags whatever the program would still give the correct result: "The factorial of 3 is 6" Learn more about Go: http://golang.org Other References: |