How To: Create a Web App using Google Go

Saturday 17 December 2011, 15:48

Google Go is an exciting new language with a lot of potential. The standard library has strong support for web apps and makes it easy to create programs that serve HTTP requests. I'm going to have a look at how to create a simple web app using Google Go.

Google Go's standard library provides the basic building blocks to create almost any kind of program. The http package contains everything you would need to serve HTTP requests in just a few lines of code. In this example we are going to make a program that listens on port 8080 and will respond with a constant chunk of HTML.

file: http.go

package main

import(
    "http"
    "log"
    "os"
)

const resp = `<html>
        <head>
        <title>Simple Web App</title>
      </head>
      <body>
      <h1>Simple Web App</h1>
      <p>Hello World!</p>
      </body>
      </html>`

func handler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte(resp))
}

func main() {
    http.HandleFunc("/", handler)
    err := http.ListenAndServe(":8080", nil)

    if err != nil {
        log.Println(err)
        os.Exit(1)
    }
}
$ 6g http.go && 6l -o http http.6
$ ./http

Here the function handler(), which accepts a parameter implementing the ResponseWriter interface and a Request struct, can read information about the request from the Request struct and write bytes to be returned to the ResponseWriter to fulfill the HTTP request.

You can see this working when you go to http://localhost:8080/ in your favorite browser. If the web app doesn't load go back to the terminal and see if an error message was printed.

While this isn't the most useful web app in the world it does get the basic point across. However we're not quite done yet, this program needs to be run from the command line before it can start serving requests. While this is good enough for development purposes, if we want our application to be used in production we need a way to start the application automatically.

Ubuntu: Upstart

Ubuntu (and derivatives like LinuxMint) come pre-packaged with a service called Upstart. A daemon for automatically starting services on system start-up and monitoring them to ensure they are restarted if they fail. Below is a simple configuration for Upstart.

file: /etc/init/go-http.conf

# Upstart Configuration

description     "Golang Web App"
author          "Jeffrey Bolle"

start on (net-device-up
          and local-filesystems
          and runlevel [2345])

stop on runlevel [016]

respawn

exec /path/to/http
$ sudo service go-http start
go-http start/running, process 12537

This simple configuration file tells Upstart to start our web app after the network and file system are ready. It also stops the web server during shutdown and monitors the process, respawning the web app in case of an unexpected exit.

You now know how to create a simple web app and set it up so that it's ready to serve requests after your server starts up. In the next post I'm going to show you how to run your web app behind an Apache server to be able to run multiple web apps from the same server / IP address.