Build a Go Boilerplate in 20 minutes (Part 1)

Build a Go Boilerplate in 20 minutes (Part 1)

Kickstart your Go application with a simple web boilerplate.

Introduction

What is Go? (No, it is not the ancient Chinese board game.) It is one of the hottest and shiny programming language to pick up now.

Go is not new. Developed in 2007 at Google by Robert Griesemer, Rob Pike, and Ken Thompson, it was finally released as an open source programming language in 2009.

Some of the major players that use Go include Uber, Medium, Facebook, Twitter, Apple and Google to name a few.

Given the rise of statically typed programming languages in recent years, it's no surprise that Go is picking up steam.

Today I will show you how to set up a Go web application boilerplate in 20 minutes or your money back.

Bonus tip at the end.

Requirements

Go (Source or Binary). Were you expecting more?

What we will be building

Initial Setup

  1. Download Go Binary or install from Source.
  2. Install Go tools and test your Go installation.

Hello World

Let's start with a simple Hello World implementation.

Create a folder in your Go root/src folder and name it Hello-World.

$ cd src && mkdir Hello-World

Create a main.go file inside the Hello-World folder.

$ touch main.go

Insert the following code in main.go.

package main

import (
    "fmt"
    "log"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello World!")
}

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

Run the program and visit http://localhost:8080/ from the browser.

$ go run main.go

A simple web server

We started by importing the package "fmt" to print Hello World! in the browser.
Next we imported the "net/http" package to handle web requests and provide HTTP client and server implementations.

The handler function sends data to the browser and writes Hello World.

Finally, the main function starts a HTTP server with port 8080 and handles all requests from the handler function.

We can add HTML to the handler function.

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "<h1>%s</h1>", "Hello World")
}

We can also get fancy and declare variables for the print statement.

func handler(w http.ResponseWriter, r *http.Request) {
    title, body := "Home", "This is the home page."
    fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", title, body)
}

Short hand declaration

In Go, we can declare variables in different ways. The syntax above ":=" is a short hand declaration.

Read more about variables from Go's documentation.

Let's add an "About" page and navigation section to the application.

Replace the code in main.go with the following.

package main

import (
    "fmt"
    "log"
    "net/http"
)

// Handler for home page
func IndexHandler(w http.ResponseWriter, r *http.Request) {
    title, body := "Home", "This is the home page."
    fmt.Fprintf(w, "<a href='/'>Home</a> <a href='/about/'>About</a><br><h1>%s</h1><div>%s</div>", title, body)
}

// Handler for about page
func AboutHandler(w http.ResponseWriter, r *http.Request) {
    title, body := "About", "This is the about page."
    fmt.Fprintf(w, "<a href='/'>Home</a> <a href='/about/'>About</a><br><h1>%s</h1><div>%s</div>", title, body)
}

// Main function
func main() {
    http.HandleFunc("/", IndexHandler)
    http.HandleFunc("/about/", AboutHandler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

Click here to view the code

Run the program again and refresh the browser.

Our application works but all these hardcoded HTML is making my eye twitch.

Continue to part 2 and learn how to extract the HTML into its own template.

Latest Posts

How Chat-GPT Replaced My JobHow Chat-GPT Replaced My Job
The Rise and Fall of AI EmpiresThe Rise and Fall of AI Empires
GPT-3: The Latest Craze in NLPGPT-3: The Latest Craze in NLP

Copyright © Terence Lucas Yap

Powered by Gatsby JS