3 ms·
Let's say you're doing something with the result of http.Get, because otherwise it makes no sense. In Go, you'd have something like: resp, err := http.Get(
by haldean 10y ago
Let's say you're doing something with the result of http.Get, because otherwise it makes no sense. In Go, you'd have something like:
resp, err := http.Get(someURL)
if err != nil {
return nil, err
}
res, err := DoStuff(resp)
if err != nil {
return nil, err
}
return res
In Haskelly-Go, you'd do:
do
resp <- http.Get(someURL)
DoStuff(res)
The types of both http.Get and DoStuff would be monadic, and the result of the do block would also be monadic. When you eventually get to the place where the error is significant, you handle it there when "unwrapping" your value from the monad.
- greenbast 10y agoThe Go code in your example could actually be written: resp, err := http.Get(someURL) if err != nil { return nil, err } return DoStuff(resp)
- marcosdumay 10y agoYeah, but if you had 5 operations in Haskell, you'd write: do a <- operation1 operation2 a -- This one returns nothing b <- operation3 a c <- operation4 b operation5 c In Go, you'll keep repeating that 'if err != nil {' everywhere. And things get way more interesting with more complex monads, but error handling is simple.