Go 1 · Code patterns

HTTP server

Start a web server with only the standard library, using method and path patterns in the router.

package main

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

func main() {
	http.HandleFunc("GET /hello/{name}", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello, %s!", r.PathValue("name"))
	})
	log.Fatal(http.ListenAndServe(":8080", nil))
}

Official Go 1 documentation

More code patterns commands

All code patterns commands · Go 1 cheatsheet

Was this page helpful?