Mark Juniper

Power Platform | MS365 | Azure


Simple Go Webserver

Published July 17, 2019

In my short mission to learn Go I’ve been looking at how to create web apps. It seems people generally favour two routes

  1. Build apps using the standard library packages
  2. Rely on a web framework

I guess this isn’t too different to working in Python (or any other language), where you have the choice to start from scratch yourself or use a framework like Flask or Django.

Frameworks allow you to build things quicker using the components others have already pieced together.

Web development using the standard packages

Web development using the standard Go library focuses on a couple of common ideas

  1. Handler functions
  2. Templating

A simple webserver

A simple webserver in Go looks as follows

package main

import (
    "fmt"
    "net/http"
)

func handler(writer http.ResponseWriter, request *http.Request) {
    fmt.Fprintf(writer, "Hey hows it going?, %s!", request.URL.Path[1:])
}

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