Comment

Comment by nemothekid

You can hit this bug without using reflect. It, to me, is Go's third biggest wart. I'm particularly fond of this bug, because I got bit by it and it crashed prod during an important sales demo.

    package main

    type Compressor interface {
        compress() []byte
    }

    type GZipComprosser struct {
        internal byte
    }

    func (c *GZipComprosser) compress() []byte {
        return []byte{c.internal}
    }

    func DoCompress(c Compressor) []byte {
        if c == nil {
            return []byte{}
        }
        return c.compress()
    }

    func main() {
        var c *GZipComprosser
        c = nil
        DoCompress(c)
    }
This is the "nil interface" trap and will crash with an NPE even though are "checking" nil.
parent nemothekid 5 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton