-3
handler := func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "<html><body>Hello World!</body></html>")
}
func
キーワードは何をしているのですか?私はツアー・オブ・ゴーを読んでいて、ここで何が起こっているのか混乱しています。
を編集:インポートリストを追加しましたし、それが離れて
それはここの機能の一部だのだったと機能:
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"
)
参照https://golang.org/ref/spec#FunctionLit –