top | item 43489408

(no title)

elvlysh | 11 months ago

> Do this call

value, err := function()

> if there is an error return it

if err != nil { return err }

> otherwise give me the value

// rest of the code goes here

discuss

order

int_19h|11 months ago

And then you forget to write the err check once out of 100 times you have to write this verbiage. And the compiler lets you, because err was already checked in that same function (but for a different call), so it's not unused anymore.

snuxoll|11 months ago

Yeah, not a huge fan of error handling in go - stuck relying on a linter to catch you and because of shadowing rules it's extremely difficult to make it look nice.

Rust's `?` operator on Result<T,E> types is flipping fantastic, puts all of the following to shame.

    // can forget to check err
    thing, err := getThing()
    if err != nil {
      panic(err)
    }

    // More verbose, now you could possible forget to assign thing
    var thing Thing
    if t, err := getThing(); err != nil {
        panic(err)
    } else {
      thing = t
    }

    // What I end up doing half the time when I've got a string of many
    // calls that may return err as a result of this

    var whatIActuallyWant string
    if first, err := getFirst(); err != nil {
      return err
    } else if second, err := doWith(first); err != nil {
      return err
    } else if final, err := doFinally(second); err != nil {
      return err
    } else {
      whatIActuallyWant = final
    }
It's actually to the point that in quite a few projects I've worked on I've added this:

   func [T] must(value T, err error) T {
     if err != nil {
       panic(err)
     } else {
       return value
     }
   }

Dylan16807|11 months ago

That's not compact.

elvlysh|11 months ago

Compactness is in the eye of your beholder. You might have the eyes of a spider, but I have the eyes of a large orc.