safetypes

Rust like result and option implementation for golang

go get github.com/eminarican/[email protected]

Examples

Option

func main() {
    if opt := test(true); opt.IsSome() {
        println(opt.Unwrap())
    } else {
        println("poor option :(")
    }
}

func test(some bool) (opt safetypes.Option[int]) {
    if some {
        return opt.Some(7)
    }
    return opt.None()
}

Result

func main() {
    if res := test(true); res.IsOk() {
        println(res.Unwrap())
    } else {
        panic(res.Error())
    }
}

func test(ok bool) (res safetypes.Result[int]) {
    if ok {
        return res.Ok(7)
    }
    return res.Err("some fancy error msg")
}

Note

Error and None methods usable as structless but it doesn’t infere types so instead of using safetypes.None[T]() and safetypes.Err[T]("") you could use them as how in examples above

GitHub

View Github