4

I've created function which needs to run in goroutine, the code is working(this is just simple sample to illustrate the issue)

go rze(ftFilePath, 2)


func rze(ftDataPath,duration time.Duration) error {

}

I want to do something like this

errs := make(chan error, 1)
err := go rze(ftFilePath, 2)
if err != nil{
    r <- Result{result, errs}
} 

but not sure how to it, most of the examples show how you do it when you using func https://tour.golang.org/concurrency/5

3
  • 1
    If you want to wait til the rze completes - then you don't need to run it in a goroutine. Just run it normally and block.
    – zerkms
    Jun 15, 2020 at 11:35
  • @zerkms - it should run in goroutines, otherwise I don't have issue :)
    – Beno Odr
    Jun 15, 2020 at 11:38
  • Your code is blocking, running it as it is shown in a goroutine makes no sense.
    – zerkms
    Jun 15, 2020 at 22:21

3 Answers 3

14

You cannot use the return value of a function that is executed with the go keyword. Use an anonymous function instead:

errs := make(chan error, 1)
go func() {
    errs <- rze(ftFilePath, 2)
}()

// later:
if err := <-errs; err != nil {
    // handle error
}
1
  • This is the way. I had to add a list of SQS queue listeners, each with their own handler function in a single processor and this way allowed me to listen for each queue in it's own goroutine with errors propagating into this error channel. Jul 13, 2022 at 8:51
7

You can use golang errgroup pkg.

var eg errgroup.Group
eg.Go(func() error {
  return rze(ftFilePath, 2)
})
if err := g.Wait(); err != nil {
    r <- Result{result, errs}
} 
2

You can handle error from go routines using a separate channel for errors. Create a separate channel specifically for errors. Each child go routine must pass corresponding errors into this channel.

This is one of the ways how it can be done:

func main() {
    type err error

    items := []string{"1", "2", "3"}

    ch := make(chan err, len(items))
    for _, f := range items {
        go func(f string) {
            var e err
            e = testFunc(f)
            ch <- e
        }(f)
    }
    for range items {
        e := <-ch
        fmt.Println("Error: ", e)
        if e != nil {
            // DO Something
        }
    }
}

func testFunc(item string) error {
    if item == "1" {
        return errors.New("err")
    }
    return nil
}
8
  • thanks, I saw this blog already. as I write it's a bit diffent as I dont use func
    – Beno Odr
    Jun 15, 2020 at 11:39
  • what are you looking for exactly? Be more specific please Jun 15, 2020 at 11:45
  • assume that you have very simple function like mine (rze), how would you catch error?
    – Beno Odr
    Jun 15, 2020 at 11:48
  • go routine function or normal function? There nothing like catch an error(as in Java) in Go. Please refer the Error interface. In go error is either nil or non nil. nil meaning no error and non nil meaning some error. golang.org/pkg/errors Jun 15, 2020 at 11:51
  • how would you catch an errors for the function rze errs := make(chan error, 1) go rze(ftFilePath, 2)
    – Beno Odr
    Jun 15, 2020 at 11:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.