Page 1 of 1

Go Introductions Introduction and how to set up compiler

#1 TMKCodes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 48
  • View blog
  • Posts: 440
  • Joined: 21-March 09

Posted 20 February 2010 - 04:26 PM

Go is new language from google developers. Go is simple, fast, safe, concurrent, open source and fun. Go compilers produce fast code fast, typical builds take a fraction of second, when whole applications run nearly as quickly as c or c++. Go is general purpose language with systems programming in mind. Programs are packages and packages allow efficient management of dependencies. Go at the moment uses general compile/link model to generate executable applications.

package main

import "fmt"

func main(){
    fmt.Printf("Hello World by Go!\n")
}



Go has compilers for Linux and Mac there is no windows support.

First you need to set few Environment variables into your .bashrc so open it.

$ gedit ~/.bashrc

Add to the end the following.

#Google Go Env Vars
export GOROOT=~/go/src
export GOOS=linux
export GOARCH=386
export GOBIN=$HOME/go/bin
PATH=${PATH}:$HOME/go/bin

Now you have to reset your .bashrc with.

$ source .bashrc
Downloading Go source

After you have done this we can download the source of go with mercurial. If you do not have it installed yet then use method for your system or you can go to mercurial.com and get it from there.

#ubuntu
$ sudo apt-get install mercurial

#Python Easy Install, requires python, python-setuptools and python-dev packages
$ sudo easy_install mercurial

Create folders.

$ mkdir go
$ cd go
$ mkdir bin

Now you can run the command that fetch the source of go from mercurial repository.

$ hg clone -r release https://go.googlecode.com/hg/ $GOROOT

This might take some time depending on your network speed, but please wait. Once you have it checkout, you can move to installing Go compiler.
Installing Go

The Go Compiler and Tools are written in C so you need to have GCC, C standard libraries, bison, make, awk and the text editor ed installed. Os X users install them as part of XCode. On Linux,

#ubuntu
$ sudo apt-get install bison gcc libc6-dev ed gawk make

To build the tool-set you have to move into the GOROOT folder.

$ cd ~/go/src/src

Now you can execute the all.bash

$ ./all.bash

If all goes well it should finish with message like this.

N known bugs; 0 unexpected bugs

N will be different version number.
If you have building problems contact people at #Go-Nuts.

We should know, Go is really fresh language and there is not much tools for it done yet. So don't think you can use awesome IDE like Visual Studio with Go, well for start, there's no windows compiler for go yet or not sure if even one comes. At the moment working with Go is painful with text editor and console, well some of the old time programmers love working this way. Go will for sure change and so will this guide accordingly. So what you can do with Go? at the moment you can easily write system utilities and server side services.

So lets start, first i will explain how the Hello World code works and then we will try compiling it.
You need to define package name for every file you create with the keyword.

package PackageName



You can include other packages in your program by importing them.

import "PackageName



Every program has an main function which is started when the program starts and ended when the program ends.

func main(){
}



Inside the main function is code that runs. In the Hello World code we are using function PrintF function from the fmt package and making it to print "Hello World by Go!NewLine" the \n creates new line, like when you press enter while editing text files.

fmt.Printf("Hello World by Go!\n")



Now we can move to compiling the Hello World code.

First create an new file called HelloWorld.go and write the hello world code into it.

package main

import "fmt"

func main(){
    fmt.Printf("Hello World by Go!\n")
}



Now you have your code. Next you have to open console and move to the folder where the code is located at.

Once you are in the same folder you can use the following commands to compile and run your code

Amd64 systems

Compile the program with this command.

$ 6g HelloWorld.go

Link it with this command.

$ 6l HelloWorld.6

Run it.

$ ./6.out

386 systems

Compile the program with this command.

$ 8g HelloWorld.go

Link it with this command.

$ 8l HelloWorld.8

Run it.

$ ./8.out

ARM systems

Compile the program with this command.

$ 5g HelloWorld.go

Link it with this command.

$ 5l HelloWorld.5

Run it.

$ ./5.out

Now you are able to compile and run your programs.

Is This A Good Question/Topic? 1
  • +

Page 1 of 1