Last active
February 2, 2022 17:29
-
-
Save dannypsnl/df86fe4291fb68fe1aa9bfb54056f6ec to your computer and use it in GitHub Desktop.
using return error in concurrency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
res := make(chan interface{}) | |
defer close(res) | |
go wrap(res, func() (interface{}, error) { | |
return os.Open("./main.go") | |
}) | |
r := <-res | |
if msg, err := r.(error); err { | |
panic(msg) | |
} | |
fmt.Printf("%s", r) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func wrap(reply chan interface{}, f func() (interface{}, error)) { | |
v, err := f() | |
if err != nil { | |
reply <- err | |
} else { | |
reply <- v | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment