Declined Proposal: A built-in Go error check function, “try”
I appreciate this change of heart, as I wouldn&#x27;t want a half-baked solution to make it into Go, but I hope they don&#x27;t just stop there.<p>What the try() proposal gets wrong is that it tries to automatically bounce the error back the stack, being equivalent to &quot;if err != nil { return }&quot;, which is ofte reasonable, but at the same time lacks the flexibility that current Go code has in terms of augmenting the error, or indeed handling it: You&#x27;d end up with try() calls for <i>some</i> things, but not all, so it&#x27;s just ironing out one wrinkle and not every wrinkle. The second argument to try() is too heavy-handed. What we need is easy, readable syntax for all cases.<p>In current Go code, people like the &quot;v, err := someFunc()&quot; syntax because it allows the happy path to stay in the current scope, with the secondary syntax &quot;if v, err :=&quot; introducing a new scope to avoid polluting the parent scope. If you&#x27;re in the current scope, your code stays flat and clean (although Go&#x27;s love of shadowing cause subtle).<p>In my opinion, we need a syntax that is similar to a &quot;catch&quot; block in other languages, but easier. Perhaps something like this:<p><pre><code>  v := getName() check err: {
    return errors.Wrap(err, &quot;could not get name&quot;)
  }
</code></pre>
This allows you to handle the error and augment it, while not introducing anything magical. It&#x27;s exactly equivalent to an &quot;if v, err :=&quot;, but without introducing a new scope <i>and</i> not polluting the current scope with an error variable. This syntax would happily support fallthrough:<p><pre><code>  v := getName() check err: {
    log.Printf(&quot;could not get name, ignoring: %s&quot;, err)
  }
  &#x2F;&#x2F; v is now the zero value
</code></pre>
And of course you could still support a syntax for introducing a new scope:<p><pre><code>  if v := getName() {
    &#x2F;&#x2F; v is now valid
  } check err: {
    &#x2F;&#x2F; You can return or anything else
  }
</code></pre>
You could easily extend it to error types with some kind of matching syntax:<p><pre><code>  v := getName() check err == io.EOF {
    &#x2F;&#x2F; On EOF
  } check err: {
    &#x2F;&#x2F; All other errors
  }
</code></pre>
The above syntaxes don&#x27;t support chaining. I&#x27;m on the fence. I like Rust&#x27;s &quot;?&quot; postfix syntax, and I think it could work:<p><pre><code>  &#x2F;&#x2F; This could be &quot;monadic&quot;; the first error short-circuits
  ceo := getPerson()?.getCompany()?.getCEO() check err: {
    return errors.Wrap(err, &quot;couldn&#x27;t get CEO&quot;)
  }
</code></pre>
But why not just let &quot;.&quot; be smart about errors and fall through like this?<p><pre><code>  ceo := getPerson().getCompany().getCEO() check err: {
    return errors.Wrap(err, &quot;couldn&#x27;t get CEO&quot;)
  }
</code></pre>
After all, &quot;.&quot; can know if the function returns multiple values, and &quot;.&quot; on a tuple (or whatever Go calls it) isn&#x27;t valid, so why not &quot;promote&quot; the &quot;.&quot; to a high-level operator here?<p>The awkwardness of the try proposal goes deeper than just error handling, too, I think. One reason Go can&#x27;t solve its error handling problem <i>elegantly</i> is because of its reliance of multiple, exclusive return types. Almost all functions with this signature:<p><pre><code>  func getName() (string, error)
</code></pre>
...obey the unwritten rule that the function returns <i>either</i> a valid value, or an error. If you get an error, the value is supposed to be unusable, and the way to check for it is to look at whether there was an error.†<p>In many type systems, a situation where a return value can be <i>either</i> A or B is variously called a union, or discriminated union, or enum, or sum type, which is the term I prefer. It&#x27;s always bugged me that Go&#x27;s designers didn&#x27;t go one step further and made sum types a first-class citizen, because I think it would fit Go rather nicely. TypeScript solves it this way:<p><pre><code>  function getName(): string | number
</code></pre>
And you can also define types this way:<p><pre><code>  type number = int | float64
</code></pre>
This, incidentally, introduces the idea of optional values:<p><pre><code>  function takesOptional(x: string | null)
</code></pre>
Back to error handling, it would be more natural for a function to declare itself this way:<p><pre><code>  func getName() string | error
</code></pre>
After all, it returns a value <i>or</i> an error. It can never be the superposition of both.<p>In this regime, anything that gets a value needs to explicitly check for what it is. &quot;switch&quot; on type would work, just like today, but that gets verbose for mere errors. So we just say that the &quot;check&quot; syntax as outlined above would work for any union that involves an error. In other words:<p><pre><code>  func getThing() Person | Company | error {
    ...
  }
  thing := getThing() check err: {
    return errors.Wrap(err, &quot;can&#x27;t get thing&quot;)
  }
</code></pre>
We can do this because we can say that &quot;error&quot; is special and known to the compiler, just like make() etc. are special.<p>Of course, sum types go beyond errors, and open other avenues that are very poorly served by Go right now.<p>---<p>† This, unfortunately, isn&#x27;t universally true. For example, io.Reader&#x27;s Write() method&#x27;s contract says that if it returns io.EOF, it may still have read a final amount of data into its buffer. Many get this wrong and ignore the data read. Which points to the problem of conventions that only <i>seem</i> like unwritten rules, but also highlights how the lack of strong type-system support creates the issue in the first place.