Standard library / net/http
http.HandlerFunc.ServeHTTP()
Calls the underlying function to serve the request. Expected stdout: 'hello\n'.
Formal Syntax & Structure
go syntaxgo
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| w | ResponseWriter | Destination writer. |
| r | *Request | Source reader. |
Working Example
Example Snippetgo
package main
import ( "fmt" "net/http" "net/http/httptest")
func main() { h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "hello") }) w := httptest.NewRecorder() h.ServeHTTP(w, httptest.NewRequest("GET", "/", nil)) fmt.Println(w.Body.String())}
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) }}