What's the elegant way of checking an error is 404 not found

1,713 views
Skip to first unread message

David DENG

unread,
Feb 10, 2016, 12:25:59 PM2/10/16
to golang-nuts
The error is returned by "net/http".Client.Get etc.

Thanks!

Tim K

unread,
Feb 10, 2016, 12:36:27 PM2/10/16
to golang-nuts
From the godoc:
"A non-2xx response doesn't cause an error."
So don't expect that you will get an error back from Get for 404.

Instead check Response.StatusCode == http.StatusNotFound, after you deal with the case when error != nil of course.


Konstantin Khomoutov

unread,
Feb 10, 2016, 12:42:13 PM2/10/16
to David DENG, golang-nuts
On Wed, 10 Feb 2016 09:25:59 -0800 (PST)
David DENG <david...@gmail.com> wrote:

> The error is returned by "net/http".Client.Get etc.

if resp.StatusCode == http.StatusNotFound {
...
}

where resp is *http.Response passed to your handler.
Reply all
Reply to author
Forward
0 new messages