from the panic you can see that you're passing to ReadAll a valid interface (non-nil type pointer) however the interface contains a nil object (nil value pointer):
io/ioutil.ReadAll(0x14ce020, 0x0, 0x0, 0x1a, 0xc420069cb0, 0x1, 0x1) to understand why there are more arguments than you expect in the function call refer to this article and the articles it links to: https://joeshaw.org/understanding-go-panic-output/ The cause for this is due to the gzip library attempting to read a header from the reader before fully creating it. If it fails it may return io.EOF, as described in this comment: https://golang.org/src/compress/gzip/gunzip.go?#L180 which will be propagated up to your handler and will cause the error checking to fail. your best source of action, in my opinion, is to treat io.EOF from gzip.NewReader() as a real error. On Thu, Apr 12, 2018 at 1:09 PM, Yaroslav Molochko <ono...@gmail.com> wrote: > Hi guys, > > I have following code: > > func handler(w http.ResponseWriter, r *http.Request) { > var reader io.Reader > switch r.Header.Get("Content-Encoding") { > case "gzip": > gz, err := gzip.NewReader(r.Body) > if err != nil && err.Error() != "EOF" { > fmt.Fprintf(w, > "error with gzip reader: %s", > err.Error()) > } > defer gz.Close() > reader = gz > case "deflate": > def := flate.NewReader(r.Body) > defer def.Close() > reader = def > default: > // just use the default reader > reader = r.Body > } > body, err := ioutil.ReadAll(reader) > fmt.Println(string(body)) > } > > non gzip requests are passing by, but gzip is always falling with panic: > 19:46:09.058697 server.go:2923: http: panic serving 127.0.0.1:63744: runtime > error: invalid memory address or nil pointer dereference > goroutine 29 [running]: > net/http.(*conn).serve.func1(0xc4200b5ae0) > /usr/local/Cellar/go/1.10/libexec/src/net/http/server.go:1726 +0xd0 > panic(0x13fe840, 0x16cfc60) > /usr/local/Cellar/go/1.10/libexec/src/runtime/panic.go:505 +0x229 > compress/gzip.(*Reader).Close(0x0, 0x1a, 0xc420069cb0) > /usr/local/Cellar/go/1.10/libexec/src/compress/gzip/gunzip.go:292 > +0x22 > panic(0x13fe840, 0x16cfc60) > /usr/local/Cellar/go/1.10/libexec/src/runtime/panic.go:505 +0x229 > io/ioutil.readAll.func1(0xc420069c00) > /usr/local/Cellar/go/1.10/libexec/src/io/ioutil/ioutil.go:30 +0x104 > panic(0x13fe840, 0x16cfc60) > /usr/local/Cellar/go/1.10/libexec/src/runtime/panic.go:505 +0x229 > compress/gzip.(*Reader).Read(0x0, 0xc420204200, 0x200, 0x200, 0x200, 0x200, > 0xc420204200) > /usr/local/Cellar/go/1.10/libexec/src/compress/gzip/gunzip.go:247 > +0x37 > bytes.(*Buffer).ReadFrom(0xc4201b78f0, 0x14ce020, 0x0, 0xc4201740a0, > 0xc420069c10, 0x10d0211) > /usr/local/Cellar/go/1.10/libexec/src/bytes/buffer.go:205 +0xa0 > io/ioutil.readAll(0x14ce020, 0x0, 0x200, 0x0, 0x0, 0x0, 0x0, 0x0) > /usr/local/Cellar/go/1.10/libexec/src/io/ioutil/ioutil.go:36 +0xb5 > io/ioutil.ReadAll(0x14ce020, 0x0, 0x0, 0x1a, 0xc420069cb0, 0x1, 0x1) > /usr/local/Cellar/go/1.10/libexec/src/io/ioutil/ioutil.go:45 +0x3e > main.handler(0x14d25c0, 0xc420202380, 0xc42013e200) > /Users/onorua/dev/go/src/github.com/onorua/gpr-edge/gpr-edge.go:83 > +0xe0 > net/http.HandlerFunc.ServeHTTP(0x149a500, 0x14d25c0, 0xc420202380, > 0xc42013e200) > /usr/local/Cellar/go/1.10/libexec/src/net/http/server.go:1947 +0x44 > net/http.(*ServeMux).ServeHTTP(0x16df780, 0x14d25c0, 0xc420202380, > 0xc42013e200) > /usr/local/Cellar/go/1.10/libexec/src/net/http/server.go:2337 +0x130 > net/http.serverHandler.ServeHTTP(0xc42009d860, 0x14d25c0, 0xc420202380, > 0xc42013e200) > /usr/local/Cellar/go/1.10/libexec/src/net/http/server.go:2694 +0xbc > net/http.(*conn).serve(0xc4200b5ae0, 0x14d2b40, 0xc42019ea00) > /usr/local/Cellar/go/1.10/libexec/src/net/http/server.go:1830 +0x651 > created by net/http.(*Server).Serve > /usr/local/Cellar/go/1.10/libexec/src/net/http/server.go:2795 +0x27b > what am I doing wrong? > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.