Go for Web Development

In this post I want to talk about using Go for full-stack development. While it’s not common to hear that a site these days doesn’t use some kind of frontend framework like React, Angular, Vue etc. there are plenty of reasons to not use one. I have been learning the Go language for a year and a half now. I have developed backend APIs using the net/http package that is built in to the standard library of Go to be consumed by vanilla JavaScript or jQuery frontends. I am currently working on building a full-stack site that uses Go’s built in template engine instead of using React or some other frontend framework. For most of my career as a developer I have always used a frontend framework. Mainly React. If I needed to create an API I would typically reach for Node and the Express framework. I even started to use TypeScript. I was really enjoying creating websites using Node/Express with React. That was until I started to learn Go. I fell in love with the language and how simplistic the language was. It doesn’t use OOP which I thought was great coming from JavaScript and using React Hooks which are basically functions that allow you to handle any kind of state management in your app by writing functions instead of the need to use classes to then write methods on. It makes for a more simplistic approach, at least in my opinion. Finding out that Go doesn’t use Classes or Objects but instead uses a functional paradigm was really nice to find out.

I made a previous post about the frameworks I have tried with Go and how I think they are great once you have the basics of Go down. This post will be all about my time spent so far with just the net/http package built in to Go.

Net/HTTP

As I have mentioned a few times in this post, the net/http package is built right into the Go standard library. It has all kinds of features which allow you to build full on sites like you would if you have used something like Ruby on Rails for Ruby or Django with Python. Or you can use it to build APIs. There is even built in routing with the package though I believe there are two third party packages that make this a bit easier and it is highly recommended to use one of these over the built in but it’s not necessary. The third party packages will be covered in another post but for reference they are Chi and Gorilla/mux. To give a very brief example of how to use the net/http package we can create a new project by opening up our terminal and following along like so:

$ cd Desktop
$ mkdir myFirstApp
$ cd myFirstApp
$ go mod init 
$ code .

The above commands change to the Desktop on your computer and create a new directory to store the app. We then change into the newly created directory and initialize a go module project. Then we open it up in our favoirte code editor. Mine happens to be VS Code but you can substitue that command out with your favorite editor if you know it or just open the directory up from insie your editor program. Next we will create a new file called main.go. Inside main.go we will write:

package main
import (
    "fmt"
    "net/http"
)

func homeHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "<h1>Hello from net/http package!</h1>")
}

func main() {
    http.HandleFunc("/", homeHandler)
    fmt.Println("Starting the server on :8080")
    http.ListenAndServe(":8080", nil)
}

Now from our terminal we can run:

$ go run main.go

Running the above command will start our server and if we go to a browser and visit localhost:8080 we will see our newly created website. You should see the following in your browser.

Hello from net/http package!

While this site doesn’t look all that great, it’s a start.

Conclusion

While this post isn’t very long I just wanted introduce you very briefly to the net/http package and how you can write roughly 15 lines of code to create a basic website. This is a very basic website as it only shows an <h1> tag saying hello but I feel it gets the point across as far as using the net/http package to create a fully functioning web page. I will be back again with the next part soon.