Simple HTTP Server using Go

I recently started working with Go and was impressed with how quickly you can stand up an HTTP server. It just takes a few lines of code to get things up and running.

package main

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

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

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

You can run this with a simple command:

go run .

And, finally, test things out:

curl http://localhost:8080

You should see this in your terminal:

Hello, world!

And that’s it!