func handler(w http.ResponseWriter, r *http.Request) { version := r.ProtoMajor fmt.Fprintf(w, "HTTP version: %d", version) }
func enforceMinVersion(next http.Handler, minVersion int) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.ProtoMajor < minVersion { http.Error(w, "HTTP version too low", http.StatusBadRequest) return } next.ServeHTTP(w, r) }) }In this example, we define a middleware function that takes a minimum version number and a http.Handler as parameters. The middleware checks the incoming request's major HTTP version against the minimum required version, and returns an HTTP 400 error if it is too low. Otherwise, it delegates handling to the next middleware or the final handler. Package Library: net/http