How to Build a URL Shortener in Go

Web
Build a Go URL shortener by mapping short keys to long URLs in an HTTP handler.

Build a URL shortener in Go by creating an HTTP handler that maps short keys to long URLs using the net/http package and a storage backend.

package main

import (
	"fmt"
	"net/http"
)

var links = map[string]string{
	"go": "https://golang.org",
}

func main() {
	http.HandleFunc("/s/", func(w http.ResponseWriter, r *http.Request) {
		key := r.URL.Path[3:]
		if target, ok := links[key]; ok {
			http.Redirect(w, r, target, http.StatusFound)
		} else {
			http.NotFound(w, r)
		}
	})
	fmt.Println("Server running on :8080")
	http.ListenAndServe(":8080", nil)
}