4 ms·
Technically, with generics, you could get a Result that is almost as good as Rust, but it is unidiomatic and awkward to write: type Result[T, E any] struct
by benrazdev 9mo ago
Technically, with generics, you could get a Result that is almost as good as Rust, but it is unidiomatic and awkward to write:
type Result[T, E any] struct {
Val T
Err E
IsErr bool
}
type Payload string
type ProgError struct {
Prog string
Code int
Reason string
}
func DoStuff(x int) Result[Payload, ProgError] {
if x > 8 {
return Result[Payload, ProgError]{Err: ProgError{Prog: "ls", code: 1, "no directory"}}
}
return Result[Payload, ProgError]{Val: "hello"}
}
- yencabulator 9mo agoNullability is unavoidable in Go because of zero values. https://go.dev/ref/spec#The_zero_value https://go.dev/ref/spec#The_zero_value