examples / apis

Go link checker

A Go service in one file: check any URL's status and latency.

gohttpapi

A link checker in plain Go: GET /check?url=... fetches the target and answers with its status code and how long the round trip took. Everything comes from the standard library, and the server reads PORT from the environment with a local default, so go run . works unchanged on your machine.

Go is a good fit for this kind of service: the compiled binary is a few megabytes, idles in single-digit megabytes of memory and starts in milliseconds, which is exactly what you want from something that scales to zero between requests.

code
main.go
package main

// A tiny link checker: GET /check?url=https://example.com answers with the
// status code and how long the request took. The standard library carries
// the whole service; the binary built from it is a few megabytes and starts
// in milliseconds.

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"
)

func main() {
	port := os.Getenv("PORT")
	if port == "" {
		port = "8080"
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "GET /check?url=https://example.com")
	})

	http.HandleFunc("/check", func(w http.ResponseWriter, r *http.Request) {
		target := r.URL.Query().Get("url")
		if target == "" {
			http.Error(w, "missing ?url=", http.StatusUnprocessableEntity)
			return
		}
		client := &http.Client{Timeout: 10 * time.Second}
		start := time.Now()
		resp, err := client.Get(target)
		elapsed := time.Since(start)
		w.Header().Set("content-type", "application/json; charset=utf-8")
		if err != nil {
			w.WriteHeader(http.StatusBadGateway)
			json.NewEncoder(w).Encode(map[string]any{"url": target, "error": err.Error()})
			return
		}
		defer resp.Body.Close()
		json.NewEncoder(w).Encode(map[string]any{
			"url":    target,
			"status": resp.StatusCode,
			"ms":     elapsed.Milliseconds(),
		})
	})

	fmt.Println("Serving on localhost:" + port)
	log.Fatal(http.ListenAndServe(":"+port, nil))
}
go.mod
module go-link-checker

go 1.26

Go beyond what seems possible.