JSON、ルーティング、埋め込み
JSON, routing and embedding
ここまでのコードは
// server.go
package main
import (
"fmt"
"net/http"
"strings"
)
type PlayerStore interface {
GetPlayerScore(name string) int
RecordWin(name string)
}
type PlayerServer struct {
store PlayerStore
}
func (p *PlayerServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
player := strings.TrimPrefix(r.URL.Path, "/players/")
switch r.Method {
case http.MethodPost:
p.processWin(w, player)
case http.MethodGet:
p.showScore(w, player)
}
}
func (p *PlayerServer) showScore(w http.ResponseWriter, player string) {
score := p.store.GetPlayerScore(player)
if score == 0 {
w.WriteHeader(http.StatusNotFound)
}
fmt.Fprint(w, score)
}
func (p *PlayerServer) processWin(w http.ResponseWriter, player string) {
p.store.RecordWin(player)
w.WriteHeader(http.StatusAccepted)
}最初にテストを書く
テストを実行してみます
成功させるのに十分なコードを書く
リファクタリング♪
最後のリファクタリング
埋め込み
欠点はありますか?
最初にテストを書く
SON文字列をテストしないのはなぜですか?
データモデリング
JSONデコード
Try to run the test
成功させるのに十分なコードを書く
エンコードとデコード
リファクタリング♪
最初にテストを書く
テストを実行してみます
テストを実行するための最小限のコードを記述し、失敗したテスト出力を確認します
成功させるのに十分なコードを書く
リファクタリング♪
最初にテストを書く
テストを実行してみます
成功させるのに十分なコードを書く
リファクタリング♪
最初にテストを書く
テストを実行してみます
成功させるのに十分なコードを書く
まとめ
最終更新