Hacker News new | past | comments | ask | show | jobs | submit login

    func try(fn func()) { fn() }
    func catch(fn func(any)) {
        if v := recover(); v != nil {
            fn(v)
        }
    }
    func throw(v any) { panic(v) }

    func fail() {
        throw("Bad things have happened")
    }

    func main() {
        try(func() {
            defer catch(func(v any) {
                fmt.Println(v)
            })
            fail()
        })
    }

Sorry.



the day the go codebase throws random panics is the day I quit the company.


So you quit the day encoding/json was written?


you wrap those properly. believe me waking up in the middle of the night because someone abused panic to throw state around, end up being called from a new goroutine, just enough to crash the entire pod, sucks even more.


You are probably thinking about (proto)reflect.


No, I am thinking of encoding/json. It uses Go's exception handlers to pass errors around, much like the code above.


Forgive me as I'm not experienced in Go. I had a look at the API reference for encoding/json[1] and performed a (very hasty) search on the source code[2].

The API reference doesn't state that panics are a part of the expected error interface, and the source code references seem to be using panics as a way to validate invariants. Is that what you're referring to?

I'm not entirely sure if the panics are _just_ for sanity, or if input truly can cause a panic. If it's the latter, then I agree - yikes.

[1] - https://pkg.go.dev/encoding/json

[2] - https://cs.opensource.google/search?q=panic&sq=&ss=go%2Fgo:s...


The use does not transcend package boundaries, but you will find its use within the package itself, which is within the Go codebase spoken of earlier.


There is evil in this world and then there's ... this :D




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: