package main

import (
        "errors"
        "log"
        "time"
)

func main() {
        err := f()
        if err != nil {
                log.Fatal(err)
        }
}

func f() (err error) {
        var rerr error

        defer func() {
                if rerr != nil {
                        err = rerr
                }
        }()

        go func() {
                rerr = longTask()
        }()

        time.Sleep(time.Second * 2)
        return errors.New("f error")
}

func longTask() error {
        time.Sleep(time.Second * 1)
        return errors.New("long task error")
}

https://play.golang.org/p/mqpT8XBbeiN

Hi, Everyone! I'm newbie in go. Help needed. 
As far as I learned, gorutines should pass "value" through channels. 
There's more safe way to handle this kind of stuffs with using waitgroup, 
context, channels....I thought, "sharing variable reference in gorutine" is 
dangerous because of problems like transacion issue. 

But what if that value is just single error? Is it still unsafe or may 
cause panic? 

"longTask" in examples is a task that I can garantee it will eventually 
end. 
I know little about low level programming. So not sure if I can write this 
kind of code. 

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/ee8a9690-bddf-48f0-ad2f-f50eae5c10f1%40googlegroups.com.

Reply via email to