HTTP GET accesses a resource through an endpoint, where the endpoint combines an HTTP method with a path. GET commonly retrieves a resource or displays its state, while POST creates a resource or requests processing. section 8.2
A resource can be identified directly by a path, such as /games/{game_id}. A GET request to that route retrieves one game's status; the identifier comes from the path rather than the request body. section 8.2.2
GET-based handlers commonly work with:
- Path parameters, which target a specific resource such as /games/{gameID}. section 8.2.2
- Query parameters, which filter or qualify a request and follow the URL after a question mark. section 8.2.2
- Idempotent behavior: repeated GET calls should return the same response and leave resources unchanged. section 8.2
Reading a query parameter in Go
For a URL such as http://localhost:8080/game?lang=en, retrieve the language with lang := r.URL.Query().Get(api.Lang). URL.Query returns url.Values, whose Get(key string) string method returns the first value associated with the key. A handler can then use the value when creating the game. section 8.6.5
...
r.HandleFunc(http.MethodPost+" "+api.NewGameRoute,
newgame.Handle) #1
r.HandleFunc(http.MethodGet+" "+api.GetStatusRoute,
getstatus.Handle) #2
...
Routes a GET request to the game-status handler with http.MethodGet and the /games/{id} path. section 8.2.2
Listing 8.15 getstatus/handler_test.go: Testing handle
func TestHandle(t *testing.T) {
req, err := http.NewRequest(http.MethodGet,
"/games/", nil) #1
require.NoError(t, err)
// add path parameter
req.SetPathValue(api.GameID, "123456")
recorder := httptest.NewRecorder() #2
Handle(recorder, req)
assert.Equal(t, http.StatusOK, recorder.Code)
assert.JSONEq(t, `{"id":"123456","attempts_left":0,"guesses":[],
"word_length":0,"status":""}`, recorder.Body.String())
}
Tests a GET request, supplies a path value, invokes the handler, and checks the HTTP status and JSON response. section 8.2.2













