0
\$\begingroup\$

I was wondering why we people don't use a generic interface instead of error type. You can see this pattern a lot in the Golang community:

func Division(a int, b int) (int, error) {
    if b == 0 {
        return 0, errors.New("divided by zero error")
    }
    return a / b, nil
}

error is not like exception (in Java), so we usually have a chain of conditional statements to check the error and stuff. In this sample I just used a generic interface for both the result and the error itself (I don't need the error type here! just nil is enough)

func Division1(a int, b int) interface{} {
    if b == 0 {
        return nil
    }
    return a / b
}

Or we can have a struct for the return type, this is more clean than to have many return types.

type Output struct {
    Result interface{}
    Error  error
}

func Division(a int, b int) Output {
    if b == 0 {
        return Output{
            Error: errors.New("divided by zero error"),
        }
    }
    return Output{
            Result: a / b,
    }
}
\$\endgroup\$
8
  • \$\begingroup\$ In your question you should explain whether you already read Effective Go, and if so, what your particular criticism is. What are the benefits and drawbacks of your approaches? After you did that, you will probably receive some more insights in the answers. Right now the question doesn't reveal what you already know and what you don't. \$\endgroup\$ – Roland Illig Aug 9 '20 at 10:31
  • \$\begingroup\$ Thanks @RolandIllig, Yeah I read the effective Go. The problem is that the community use this style a lot and this can not be always good. When we don't need the error details and just a nil is enough there is no need to use error for return. and also when we need error + result then there is no need to have two return types one struct is enough (in terms of clean code). \$\endgroup\$ – coditori Aug 10 '20 at 20:30
  • \$\begingroup\$ I said "in your question" intentionally. Having this information in a comment alone has no value. \$\endgroup\$ – Roland Illig Aug 10 '20 at 20:55
  • 1
    \$\begingroup\$ And please explain in detail why you think that a struct would be cleaner than two return values. Just saying "I know it" or "this book says it" does not count. \$\endgroup\$ – Roland Illig Aug 10 '20 at 20:58
  • \$\begingroup\$ returning interface{} is problematic because you need that many additional conditional statements to figure what was returned. returning a user defined struct as you did work but it just does not take advantages of the language capabilities. Your Output type, as of now, can not provide a standard way to handle errors, it offers no contract surface to build an api upon. Consider that maintainers and creators of this language tries to provide standard ways to write program that every body can learn and apply to share and grab code with/from others. \$\endgroup\$ – mh-cbon Aug 11 '20 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.