HN user

a-poor

56 karma

Fullstack developer living in Los Angeles, CA.

https://austinpoor.com

Posts6
Comments5
View on HN
Why Go Can't Try 5 months ago

What is broken about the go error type? If anything the fact that it is a simple interface makes the `try` syntax sugar more doable – right?

Let's say you have this:

``` part, err := doSomething() if err != nil { return nil, err }

data, err := somethingElse(part) if err != nil { return nil, err }

return data, nil ```

Then as long as your function followed the contract 0+ returns and then 1 `error` return, that could absolutely be turned into just the 0+ returns and auto-return error.

The fact that the `Error` interface is easy to match and extend, plus the common pattern of adding an error as the last return makes this possible.

What am I missing here?

This means you can't pass variables in as function arguments. Even the example in the official go docs doesn't handle the scope correctly:

  func main() {
   var wg sync.WaitGroup
   var urls = []string{
    "http://www.golang.org/",
    "http://www.google.com/",
    "http://www.example.com/",
   }
   for _, url := range urls {
    // Launch a goroutine to fetch the URL.
    wg.Go(func() {
     // Fetch the URL.
     http.Get(url)
    })
   }
   // Wait for all HTTP fetches to complete.
   wg.Wait()
  }
https://pkg.go.dev/sync#example-WaitGroup

You need to use this pattern instead:

   for _, url := range urls {
    url := url
    // ...