Powerful Command-Line Applications in Go: 'go vet' reports error (server_test.go)

In chapter 8, Talking to REST APIs, section Testing the REST API Server, the following snippet in server_test.go triggers a warning from go vet:

			r, err := http.Get(url + tc.path)
			defer r.Body.Close()
			if err != nil {
				t.Error(err)
			}

‘using r before cheking for errors’. To clear this warning, we must move the defer statement after the error check like this:

			r, err := http.Get(url + tc.path)
			if err != nil {
				t.Error(err)
			}
			defer r.Body.Close()
1 Like

Thank you for catching it. It has been fixed now.

1 Like