Skip to content

Comment on Go: building on the shoulders of giants and stepping on a few toes [video]parent

Comments

I don't know, most of the time, I add information to the error before propagating it:

    if f, err := os.Open(path); err != nil {
        return nil, fmt.Errorf("could not open path: %v", err)
    }
This is the vast majority of my error management code in go. Is rust less verbose in this regard? AFAIK, the question mark operator is equivalent to `if err != nil { return nil, err }`.

If you want to transform the error on it, you can either:

1. Define a conversion between the two errors that adds the context.

2. Use .map_err() at the callsite to do the transformation there.

Thank you Steve. I'll have a look at map_err().

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.