On Sun, Sep 30, 2018 at 3:47 PM Hemant Singh <hemanti...@gmail.com> wrote:

> I have a question.  I have changed the open goping.go code to return error
> from functions.
>
> The following function is implemented in a .goping.go file.
>
> func ping(config *Config, address, name string, pattern *regexp.Regexp)
> error {  }
>
> Another function shown below, in the same .go file, calls 'go ping()'.  As
> one can see above, the ping function returns an error.  How do I change the
> 'go ping()' code below to grab the error returned from calling ping()?  I
> could also use a URL to where a description exists for using 'go
> function_call' inside a Go program.
>
> func ping_main(address string) error {
>     go ping(config, address, "DstIP", LATENCY_PATTERN)
> }
>

In this current form, it is not possible to grab the error because you are
not waiting on the ping() to finish when you call it asynchronously in a
goroutine. If you need the error, the most obvious thing to do is to call
it in a blocking fashion:

func ping_main(address string) error {
    return ping(config, address, "DstIP", LATENCY_PATTERN)
}

​

But if you need to call it in a non-blocking way, then you have to arrange
a way to receive the error value later once the ping is complete. Wrapping
the existing ping() function might be a way to go:

func ping_main(address string) <-chan error {
    err := make(chan error, 1)
    go func() {
        err <- ping(config, address, "DstIP", LATENCY_PATTERN)
    }()
    return err
}

​

This means the ping still runs in a non-blocking way, but will report the
return value when it is complete.


>
> Best,
>
> Hemant
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to