Skip to content

Instantly share code, notes, and snippets.

@dannypsnl
Last active February 2, 2022 17:29
Show Gist options
  • Save dannypsnl/df86fe4291fb68fe1aa9bfb54056f6ec to your computer and use it in GitHub Desktop.
Save dannypsnl/df86fe4291fb68fe1aa9bfb54056f6ec to your computer and use it in GitHub Desktop.
using return error in concurrency
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)
}
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