1

In Go, is it possible to return using a function only?

For example, I want to check for errors and instead of repeating an if statement, I want to just call a function such as checkErrors(err).

Here is an example (goplayground)

func foobar(str string) err {
    _, err := ioutil.ReadAll(resp.Body)
    checkError(err) // If error != nil then foobar will return the err
    /*if err != nil {
        return err
    }*/
}

func checkError(err error) {
    if err != nil {
        // Then make the function this is being called within return the err.
    }
}
6
  • if I understand the question correctly, then it is not possible Commented Apr 9, 2016 at 14:22
  • My tutor who is teaching me go said not to repeat code and I was writing a long function which kept checking for errors. I was just hoping there was a way for me to minimize my code repetition. Commented Apr 9, 2016 at 14:23
  • 1
    maybe you should read this: blog.golang.org/error-handling-and-go Commented Apr 9, 2016 at 14:25
  • 1
    @Acidic, some repetition while handling errors explicitly can make code easier to reason about. Commented Apr 9, 2016 at 14:27
  • may be correct title for this Q is:Check Error Value Using Another Function with Golang? Commented Apr 9, 2016 at 15:57

2 Answers 2

1

Your tutor is correct: don't repeat code. It is called the DRY principal.

But the authors of Go have taken a different direction when designing the language. And it has fractured the development community, it has caused wars, it has warped space and time.

One of these directions is to accept a little bit of copying, to make the code easier to read. Error handling, as you have noticed, is one such area. It invites a little repetitous pattern of checking for nil all over your app instead of abstracting away (and easily able to ignore) the true error handling you should be doing.

Rob Pike addressed this direction in his Design (and Philosophy) of GoLang talk:

http://talks.golang.org/2012/splash.article

If errors use special control structures, error handling distorts the control flow for a program that handles errors. The Java-like style of try-catch-finally blocks interlaces multiple overlapping flows of control that interact in complex ways. Although in contrast Go makes it more verbose to check errors, the explicit design keeps the flow of control straightforward—literally.

Not everyone agrees and it's why wars are started.

But, these decisions to shake up the industry standards that has put so much if a burden on developers is exactly why they are doing it: you are forced to deal with each error up front and center, every time. You cannot ignore or let something else handle it.

Not everyone agrees.

I highly suggestion you and your Tutur read that post about the design of Go. Then you two can make the decision of rather Go is the way to Go or not.

Sign up to request clarification or add additional context in comments.

2 Comments

And I'm all in with Go now, having left Java and MS after seeing the light. I tried to keep the answer netrual and not opinion based.
Very descriptive answer, I appreciate all the content you've provided. I'll just continue with repetitive error checking.
1

Just to add to the already great answer, you can make your checkError function more customized, so you get to still deal with the error up front while still keeping it DRY:

func checkError(err error, handler func(e error)) {
    if err != nil {
        handler(err)
    }
}

This way you can pass in the handler function you want to use with each error without forgetting the responsibility while saving two lines of repetitive error-handling code.

handler1 = func(e error) { 
        panic(e) 
}

handler2 = func(e error) {
        log.Fatal(e)
}

func foobar(str string) err {
    _, err := ioutil.ReadAll(resp.Body)
    checkError(err, handler2)
}

1 Comment

I haven't looked at it this way before. Great approach at removing repetition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.