Intro to Golang

How to make Web Apps: Run HTTP Server in Go
Share this page:

About Go

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software. You can use the official page to test your code live.

Before we start with Unicorn stuff, I’d strongly suggest you go to the beginners tutorial if you have no background in Go. It shouldn’t take you more then 30 minutes, and you’ll at least understand some basic concepts, and gain some basic ability to read the code.

Go Packages

Every Go program is made up of packages. A package is a collection of source files in the same directory that are compiled together. Every Go source file begins with package packageName. Go programs start running in the main package. It is a special package that is used with programs that are meant to be executable.

In Unicorn Pursuit, main.go imports 3 packages:

  • net/http, cause we’re running a web server.
  • html/template, cause we’re making a web app, and using HTML Templates.

as shown in the code below:

package main

import (
 "net/http"
 "html/template"
)

Functions and variables defined in one source file are visible to all other source files within the same package.

A module is a collection of related Go packages that are released together, and stored in a directory with a go.mod file at its root.

Go Templates

Templates are a powerful method to customize output. In Unicorn Pursuit, we will be using html/template package.

Go’s html/template package provides a rich templating language for HTML templates. It is mostly used in web applications to display data in a structured way in a client’s browser. One great benefit of Go’s templating language is the automatic escaping of data. There is no need to worry about XSS attacks as Go parses the HTML template and escapes all inputs before displaying it to the browser.

We won’t go deep into Template code here, as we’ll be using Gin Framework, so the code changes. Priciple however, stays the same. In your HTML you will use something like <h1>{{.PageTitle}}</h1> to reference a PageTitle: "Unicorn Pursuit" variable defined in your Go code. For now, thats all you need to know.

HTTP Server and Routing

Routes in Go are one of the biggest differentiators. With Routes you can create your API Gateway functionality within Go code.

Writing a basic HTTP server is easy using the net/http package.A fundamental concept in net/http servers is handlers. A handler is an object implementing the http.Handler interface.

type Server interface {
	Router() http.Handler
}

Functions serving as handlers take a http.ResponseWriter and a http.Request as arguments. This is where it gets interesting. We can define http handle functions for each route we want to serve. If we just want our Server to listen on the port 8080, and respond with Hello World, functions we need in the main.go would look something like this:

func main() {

     // tells the net.http package to handle all requests to the web root with the HelloServer function.
    http.HandleFunc("/", HelloServer)

    // serve http on port 8080
    http.ListenAndServe(":8080", nil)
}

func HelloServer(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, World")
}
  • http.Request is a data structure that represents a client HTTP request.
  • http.ResponseWriter sends data we wrote to the HTTP client.

Workshop

Even though we won’t be using http.Request and http.ResponseWriter, but Gin framework instead, it would be useful to at least go through the Web App Tutorial (URL in the References below).

Where to find more info




Last modified May 27, 2020: UpgradeCDKupdated (bbc1f55)