Installing Google Go on Ubuntu 10.04 and building a simple Web Server

Post to Twitter

The Go programming language was introduced a while back and caught a lot of attention and excitement. As the website explains, Go is a simple, fast, concurrent, safe and open source systems programming language. I decided recently to give Go a whirl and installed it on Ubuntu 10.04. Today I’ll point out a few tips I found along the way of installing, building and running Go.


I used VMWare with an instance of Ubuntu 10.04 running. Following the installation instructions was pretty straight forward. I installed the C tools first as instructed without any issue.

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

The next step was to fetch the repository. Here I had to install the appropriate Python tools as well as some other tools, some were already installed like “ed” and “gawk”.

$ sudo apt-get install python-setuptools python-dev build-essential gcc

Once that was done I was able to then run the Python easy_install for Mercurial:

$ sudo easy_install mercurial

Once that was finished you go and fetch the Go repository:

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

I actually had that hang on me so I did a “CTRL-C” and tried it again and it worked the second time.

Building Go:

$ cd go/src
$ ./all.bash

My results were not the same as the Go install page, instead I got this:
“1 known bugs; 0 unexpected bugs”

Also, I built this on an Intel based system so it obviously didn’t build for amd64 but instead it built as: 386 (x86 x86-32). Make sure you pay attention to that since it will determine what your Go commands will be.

At this point there should be a little note at the bottom stating you need to add the Go bin directory to your PATH. I used Pico and opened my .bashrc file and added this to it:

export PATH=$PATH:/home/clung/go/bin

Make sure to set it according to your settings. Exit terminal and then go back into it to ensure the new path has taken effect.

The first example they offer is easy enough but believe it or not I messed it up.

package main

import "fmt"

func main()
{
	fmt.Printf("hello, world\n")
}

Saving this code as “hello.go” and then compiling it will give an error as seen here:

Guess what I did wrong? Yep, I put the brace on the line after “func main”. Apparently with Go you cannot place your opening brace on a new line.

So I fixed the error:

package main

import "fmt"

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

To run the program you need to compile and link (this is where you quickly begin to scheme in your head of using your own Makefiles to make this process easier/quicker). The process is pretty simple for this Hello World program.

Notice that I used the 8g and 8l tools and not the 6g and 6l ones since I am using the “386″ build of Go.

Since I’m really into in things like Mobile (Android), Flash/Flash Builder/Silverlight and the Web (HTML 5, PHP, ASP.NET, Servlets, etc.) I thought the next program should be a web server. I used some of the Go documentation for the “ListenAndServe” function for the HTTP package and modified it just a little.

package main

import (
	"http"
	"io"
	"log"
)

const templateStr = `
<html>
<head>
<title>Hello, Go</title>
</head>
<body>
<br /><br /><br />
&nbsp;&nbsp;&nbsp;Hello, Go!
</body>
</html>
`

func HelloServer(c *http.Conn, req *http.Request) {
	io.WriteString(c, templateStr)
}

func main() {
	http.HandleFunc("/", HelloServer)
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		log.Exit("ListenAndServe: ", err.String())
	}
}

I saved this file as “goweb.go”.

Compiling and linking is still just as easy as the hello world program:

Using Chrome I then browsed to: http://localhost:8080

Voila! Instant Go Web Server.

Post to Twitter

This entry was posted in Go, Open Source, Ubuntu. Bookmark the permalink.

3 Responses to Installing Google Go on Ubuntu 10.04 and building a simple Web Server

  1. marksteve says:

    its very nice step by step installation guide, only the setting of environmental variable is a bit hard especially for beginners like me. thanks, i really configure this one out using Ubuntu 10.04 without a problem…

  2. Matt Parlane says:

    Just in case anyone finds this and tries to make it work (like I did), here’s an up-t0-date version (with HTML5 goodness as well):


    package main

    import (
    "http"
    "io"
    "log"
    )

    const templateStr = `

    Hello, Go

    Why, hello there.

    `

    func HelloServer(w http.ResponseWriter, req *http.Request) {
    io.WriteString(w, templateStr)
    }

    func main() {
    http.HandleFunc("/", HelloServer)
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
    log.Fatal("ListenAndServe: ", err.String())
    }
    }

    Not The Chad’s fault, the Go packages are fairly immature and still change every now and then, I’m sure they’ll settle down soon though.

    Matt

  3. Matt Parlane says:

    grr!! stripped my tabs.

Comments are closed.