Failures
? for propagation
Section titled “? for propagation”On a Result, ? unwraps Ok on success, and returns early with Err on failure:
fn read_config(path: string) -> Result<Config, error> { let bytes = os.ReadFile(path)?on success, bytes is the value inside Ok
on failure, read_config returns Err parse_config(bytes)}Equivalent without ?:
fn read_config(path: string) -> Result<Config, error> { let bytes = match os.ReadFile(path) { Ok(contents) => contents, Err(failure) => return Err(failure), } parse_config(bytes)}On an Option, ? unwraps Some when present, and returns early with None when absent:
fn get_name(id: int) -> Option<string> { let user = users.get(id)?when present, user is the value inside Some
when absent, get_name returns None Some(user.name)}Equivalent without ?:
fn get_name(id: int) -> Option<string> { let user = match users.get(id) { Some(found) => found, None => return None, } Some(user.name)}Attaching context to errors
Section titled “Attaching context to errors”? propagates an error unchanged. To record where it passed through, wrap_err() prepends a message to the error while keeping the original as the cause:
fn read_config(path: string) -> Result<Config, error> { let bytes = os.ReadFile(path).wrap_err("reading config file")? parse_config(bytes)}An Ok passes through unchanged. An Err keeps the original error inside, so errors.Is and errors.AsType still match it:
fn start() { let Err(err) = read_config("/etc/app.conf") else { return } fmt.Println(err)prints reading config file: open /etc/app.conf: no such file or directory fmt.Println(errors.Is(err, fs.ErrNotExist))prints true}try blocks
Section titled “try blocks”A try block groups fallible calls and evaluates to a Result or an Option. Inside it, ? returns from the block rather than from the function:
fn load_config(path: string) -> Config { let result = try { let file = os.ReadFile(path)?failure propagates to result parse_config(file)? }
match result { Ok(config) => config, Err(err) => { fmt.Println(f"using default config: {err}") Config { .. } }, }}Defining your own errors
Section titled “Defining your own errors”Any type with an Error() method can be returned as an error:
struct ValidationError { field: string, message: string,}
impl ValidationError { fn Error(self) -> string { f"{self.field}: {self.message}" }}
fn validate(input: Input) -> Result<Input, ValidationError> { if input.name == "" { return Err(ValidationError { field: "name", message: "required" }) } Ok(input)}It then propagates with ? into any function returning error:
fn process(input: Input) -> Result<Input, error> { let valid = validate(input)?ValidationError satisfies error Ok(valid)}Partial results
Section titled “Partial results”Where a Go function can return a value and an error at the same time, such as io.Reader.Read, the result is a Partial<T, E>. Handle its three cases with match:
fn count_bytes(reader: io.Reader) -> Result<int, error> { let mut buf = Slice.make<byte>(4) let mut total = 0 loop { match reader.Read(buf) { Partial.Ok(n) => total += n, Partial.Err(err) => return Err(err), Partial.Both(n, err) => {read n bytes, then hit err total += n if errors.Is(err, io.EOF) { return Ok(total) } return Err(err) }, } }}An error is an interface value, so == is refused on it. Compare with errors.Is, which also matches an error wrapped by wrap_err().
No unwrap()
Section titled “No unwrap()”Lisette omits Rust’s unwrap(). To unwrap a value, use:
?to propagate failurematchto handle every caselet elsefor early exitunwrap_or()with a default
Panic recovery
Section titled “Panic recovery”To catch panics at runtime, Go uses recover() in a deferred anonymous function:
defer func() { if r := recover(); r != nil { log.Println(r) }}()handleConnection(conn)Lisette’s recover block serves the same purpose. It yields a Result, and its Err holds a PanicValue that carries what the panic passed:
let result = recover { handle_connection(conn)}
if let Err(pv) = result { log.Println(pv.message())}