Standard library / net/http
http.ServerContextKey
Context key used in a live HTTP handler to retrieve *http.Server. Do not use as an application-specific context key. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
var ServerContextKey = &contextKey{"http-server"}Working Example
Example Snippetgo
package main
import ( "fmt" "io" "net/http" "net/http/httptest")
func main() { s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, ok := r.Context().Value(http.ServerContextKey).(*http.Server) fmt.Fprint(w, ok) })) defer s.Close() res := must(s.Client().Get(s.URL)) defer res.Body.Close() fmt.Println(string(must(io.ReadAll(res.Body))))}
func must[T any](v T, e error) T { if e != nil { panic(e) } return v}func check(e error) { if e != nil { panic(e) }}