No description
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Isaev Denis 43b3ff1b0d
Merge pull request #1 from stevenh/method-panic
fix: Panic on nil Call.Method
2019-09-24 12:11:05 +03:00
.circleci /go/src/github.com/timakin/bodyclose 2019-03-26 12:50:52 +09:00
passes/bodyclose fix: Panic on nil Call.Method 2019-09-24 08:29:42 +00:00
.gitignore Initial commit 2019-03-23 11:09:49 +09:00
.golangci.yml fix: lint yml 2019-03-30 00:01:17 +09:00
go.mod update go mod 2019-03-25 23:33:25 +09:00
go.sum update go mod 2019-03-25 23:33:25 +09:00
LICENSE Initial commit 2019-03-23 11:09:49 +09:00
main.go break 2019-03-27 12:47:56 +09:00
main_go112.go pass lint without skip-files 2019-03-26 13:26:15 +09:00
README.md Update README.md 2019-03-26 15:43:42 +09:00

bodyclose

CircleCI

bodyclose is a static analysis tool which checks whether res.Body is correctly closed.

Install

You can get bodyclose by go get command.

$ go get -u github.com/timakin/bodyclose

How to use

bodyclose run with go vet as below when Go is 1.12 and higher.

$ go vet -vettool=$(which bodyclose) github.com/timakin/go_api/...
# github.com/timakin/go_api
internal/httpclient/httpclient.go:13:13: response body must be closed

When Go is lower than 1.12, just run bodyclose command with the package name (import path).

But it cannot accept some options such as --tags.

$ bodyclose github.com/timakin/go_api/...
~/go/src/github.com/timakin/api/internal/httpclient/httpclient.go:13:13: response body must be closed

Analyzer

bodyclose validates whether *net/http.Response of HTTP request calls method Body.Close() such as below code.

resp, err := http.Get("http://example.com/") // Wrong case
if err != nil {
	// handle error
}
body, err := ioutil.ReadAll(resp.Body)

This code is wrong. You must call resp.Body.Close when finished reading resp.Body.

resp, err := http.Get("http://example.com/")
if err != nil {
	// handle error
}
defer resp.Body.Close() // OK
body, err := ioutil.ReadAll(resp.Body)

In the GoDoc of Client.Do this rule is clearly described.

If you forget this sentence, a HTTP client cannot re-use a persistent TCP connection to the server for a subsequent "keep-alive" request.