Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8049591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:49:29+00:00 2026-06-05T06:49:29+00:00

Let us say we want to implement following computation: outval / err = f3(f3(f1(inval))

  • 0

Let us say we want to implement following computation:

outval / err = f3(f3(f1(inval))

where each of f1, f2, f3 can fail with an error at that time we stop the computation and set err to error returned by the failing function. (Of course, nesting can be arbitrarily long)

In languages like C++/JAVA/C# it can be easily done by having f1, f2 and f3 throw an exception and enclosing the computation in a try-catch block, while in languages like Haskell we can use monads instead.

Now I am trying to implement it in GO and the only approach I can think of is obvious if-else ladder which is rather verbose. I don’t have issue if we can’t nest the calls, but in my opinion adding error check after each line in code looks ugly and it breaks the flow. I would like to know if there is any better way of doing it.

Edit: Editing as per the comment by peterSO
Below is the concrete example and straightforward implementation

package main

import "fmt"

func f1(in int) (out int, err error) {
    return in + 1, err
}

func f2(in int) (out int, err error) {
    return in + 2, err
}

func f3(in int) (out int, err error) {
    return in + 3, err
}

func calc(in int) (out int, err error) {
    var temp1, temp2 int
    temp1, err = f1(in)
    if err != nil {
        return temp1, err
    }
    temp2, err = f2(temp1)
    if err != nil {
        return temp2, err
    }
    return f3(temp2)
}

func main() {
     inval := 0
     outval, err := calc3(inval)
     fmt.Println(inval, outval, err)
}

What I am trying to illustrate is, function calc does some computation possibly with the help of library functions that can fail and semantics is if any call fails calc propagates the error to the caller (similar to not handling the exception). In my opinion, code for calc is ugly.

Between for this particular case where all library functions have exactly same signature, we can make the code better (I am using idea from http://golang.org/doc/articles/wiki/#tmp_269)

func saferun(f func (int) (int, error)) func (int, error) (int, error) {
    return func (in int, err error) (int, error) {
        if err != nil {
            return in, err
        }
        return f(in)
    }
} 

Then we can redefine calc as

func calc(in int) (out int, err error) {
    return saferun(f3)(saferun(f2)(f1(in)))
}

or as

func calc(in int) (out int, err error) {
    sf2 := saferun(f2)
    sf3 := saferun(f3)
    return sf3(sf2(f1(in)))
}

But without generics support, I am not sure how I can use this approach for any set of library functions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:49:31+00:00Added an answer on June 5, 2026 at 6:49 am

    The discussion between Errors vs Exceptions is a long and tedious one. I shall therefore not go into it.

    The simplest answer to your question concerns Go’s built-in defer, panic, and recover functions as discussed in this blog post. They can offer behaviour similar to exceptions.

    package main
    
    import "fmt"
    
    func main() {
        defer func() {
            // This recovers from a panic if one occurred. 
            if x := recover(); x != nil {
                fmt.Printf("%v\n", x)
            }
        }()
    
        value := f(f(f(1)))
        fmt.Printf("%d\n", value)
    }
    
    func f(i int) int {
        value := i*i + 1
    
        // something goes wrong, panic instead of returning an error.
        panic("ohnoes")
    
        return value
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I want to implement a function that is supposed to process an
Let's say I want to extract a list of sections from an email that
I'm trying to learn interfaces and want to try the following: Let's say I
Let's say that I've got the following class hierarchy : public static class Constants
I wonder if the following redirection is possible: Let's say that a smart phone
For some strange reason, let's say that I want use List Comprehensions -- and
Let's say I want to create a collection class that is thread-safe by default.
I have the following (imperative) algorithm that I want to implement in Haskell: Given
Let say i want to read the integers a, b and c from stdin
let say I want to do this: var dashboard = {}; var page =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.