by haldean · 10y ago · 0 comments
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. resp, err := http.Get(someURL)
if err != nil {
return nil, err
}
return DoStuff(resp) 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.