Standard library / net/http
http.LocalAddrContextKey
Context key used in a live HTTP handler to retrieve net.Addr. Do not use as an application-specific context key. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
var LocalAddrContextKey = &contextKey{"local-addr"}Working Example
Example Snippetgo
package main
import ( "fmt" "io" "net" "net/http" "net/http/httptest")
func main() { s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, ok := r.Context().Value(http.LocalAddrContextKey).(net.Addr) 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) }}