Go Programming Language Introduction

Golang, is a programming language initially developed at Google. One of it's creators is Ken Thompson , one of the creators of C Programming Language. It is a statically-typed language with syntax loosely derived from that of C, adding garbage collection, type safety, some dynamic-typing capabilities, additional built-in types such as variable-length arrays and key-value maps, and a large standard library. In this post We will cover installation, and the construtcion of a basic website.

Installation

We will install and configure debian based system. You can referd to official documentation where instructions for other platforms are availabe.

Installing the language

  $ sudo apt-get install golang  

Installing the tools

After install the language you must download the tools file and extract it into /usr/local

  $ wget https://storage.googleapis.com/golang/go1.3.3.linux-amd64.tar.gz
  $ sudo mv go1.3.3.linux-amd64.tar.gz /usr/local/
  $ cd /usr/local
  $ sudo  tar -xzf go1.3.3.linux-amd64.tar.gz

Add /usr/local/go/bin to the PATH environment variable

    export PATH=$PATH:/usr/local/go/bin

Hello world

Create a file named hello.go and copy the code:

  package main
  import "fmt"

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

Run the program:

  $ go run hello.go  

Simple Website

We will use the standard http package to write a simple httpserver, first at all We will create a file named server.go and Wi will add the next lines:

    package main

    import (
        "fmt"
        "io/ioutil"
        "net/http"
    )

Now we will create an struct where to store every page:

    type Page struct {
        Title string
        Body  []byte
    }

Every page is represented by a title tht is a string and the body that will be represented as a slide (sequence) of bytes. A detailed explanation of slides can be found at this link.

Now we create a method that will read the body from a text file, for simplicity We assume that filename is the page's title.

    func loadPage(title string) (*Page, error) {
        filename := title + ".html"
        body, err := ioutil.ReadFile(filename)
        if err != nil {
            return nil, err
        }
        return &Page{Title: title, Body: body}, nil
    }

Now we add the code that will handle http request and will return the html content:


    func viewHandler(w http.ResponseWriter, r *http.Request) {
        title := r.URL.Path[len("/view/"):]
        p, _ := loadPage(title)
        fmt.Fprintf(w, "%s",  p.Body)
    }

Finally, We will add the main method, where We create the http server:

    func main() {
        http.HandleFunc("/view/", viewHandler)
        http.ListenAndServe(":8080", nil)
    }

We can build the program with the command go build server.go and then running it just typing ./server

You can test the server with a simple curl request:

    $curl http://localhost:8080/view/go-intro

You can see the source code at this link

References