Hacker News new | past | comments | ask | show | jobs | submit login

I don't see how a switch statement helps you avoid checking for errors when methods you use return errors, and a lot of methods return errors.



You can pass functions to functions (https://play.golang.org/p/XNMtrDUDS0).

So you can do (sans syntax):

    ErrorCheckerFunc(fn myParams -> MyFunc(myParams), "message")

    ErrorCheckerFunc(fn myParams2 -> MyFunc2(myParams2), "message2")
etc...

and then the actual function:

    ErrorCheckerFunc(fn myFunc, message){
     returnVal, returnError = myFunc();
     switchError:
      case error is foo:
       print error
      case error is bar: 
       log error print "error logged"
      ...
      case n+1 etc

      // you can handle message in a similar way to get 
      // cases by function names, and if you have elixir
      // style quote unquote metaprogramming in Golang you might just be able to get function names based on passed function - I don't think this is in the language spec though it may be possible to do some other way.

      if error not nil do switchError(error)
       return RuhRoh
      else
       return returnVal
    }
It's not so bad, it's just a middleware.


I'm not clear on what exactly you're trying to represent here, but it looks like something that wouldn't work with Go's type system. ErrorCheckerFunc would need to always have the same return type and accept a function with the same type signature.


If only there was a way to make a single function work with multiple different types. A way to "genericize" it, you might say.

Alas, that's obviously completely impossible. Such a thing is beyond the capabilities of us mortal programmers. But maybe one day PL researchers will discover a way. One day...


Rob Pike would rather use a for loop than map or filter.. so yeah don’t dream too big

https://github.com/robpike/filter


That's kind of awesome code though. I learned things about go looking through how he implemented reduce. Thank you for sharing it.




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: