In this bit of code:
handler := func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "<html><body>Hello World!</body></html>")
}
What is the func
keyword doing? I've been reading through the Tour of Go and I'm confused as to what is going on here.
EDITED: Added import list and function that it was apart of
It's part of a function here:
func ExampleResponseRecorder() {
handler := func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "<html><body>Hello World!</body></html>")
}
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(resp.StatusCode)
fmt.Println(resp.Header.Get("Content-Type"))
fmt.Println(string(body))
// Output:
// 200
// text/html; charset=utf-8
// <html><body>Hello World!</body></html>
}
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
)