3 ms·
Oh I wish Golang had something similar to Rust's Options and Results. This `nil` value is the source of many crashes in applications I've audited (due to applic
by baby 7y ago
Oh I wish Golang had something similar to Rust's Options and Results. This `nil` value is the source of many crashes in applications I've audited (due to applications forgetting to verify if the value is `nil` before acting on it).
Maybe something like this could be supported natively in Golang:
func thing(arg bool) Error.Result(bool, error) {
if arg {
return Error.Ok(false)
}
return Error.Error(fmt.Errof("nope"))
}
func main() {
if thing() == true {} // doesn't compile
match thing(false) {
Error.Ok(value) => {
fmt.Println(value)
},
Error.Error(err) => {
log.Panic(err)
}
}
}
just for fun I wrote an ugly PoC for bool options: https://github.com/mimoo/Bool https://github.com/mimoo/Bool
- omegabravo 7y agosimilar to https://golang.org/pkg/database/sql/#NullBool https://golang.org/pkg/database/sql/#NullBool
- baby 7y agothis has a Value() getter though, but at least you would probably not make the mistake of not checking the error here.