A Short Introduction to net/http

The net/http package is part of Go’s standard library and provides everything you need to build HTTP servers (and make HTTP requests).

With net/http, you can easily spin up a basic server in just a few lines of Go. For example:

// Simple net/http server example
package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Hello, Go web!")
}

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

When you run this code, your Go program starts listening on port 8080, and any GET request to http://localhost:8080/ will return “Hello, Go web!”.

If you want to run a web server in Go, there are a wide variety of frameworks. Most of them still have their place and purpose - however for most use cases net/http offers the best development experience. If you're a new web developer, stick to net/http, but if you're more familiar with Chi/Fiber, you can easily transfer your skillset for this tutorial.

Below are a few resources to help you dive deeper: