Hacker News new | ask | show | jobs
by rakoo 4241 days ago
> The moment you try to use one of them that is actually an error (trying to pass it as an argument to another function without inspecting it first for example), it causes your current function to return an error.

I've seen another way to do it, that works with current Go, in a redis client [1]:

- Function 1 returns rawarg, error where rawarg can be anything

- You want to transform arg into some type, so you create a function that takes a rawarg and an error and returns your type and an error

- In the implementation of your 2nd function, if err != nil, return it directly

This way, as a library user you can just chain your calls without the tedious if err != nil (it will be taken care of in the library).

This might not scale to huge programs, but there certainly is a way to reuse this idea.

[1] https://github.com/garyburd/redigo/blob/master/redis/reply.g...

1 comments

I'll have to take a look at that. Thanks for the link.