func Send(c chan T, t T) error {
var err error
defer func() {
if x := recover(); x != nil {
err = fmt.Errorf("Unable to send: %v", x)
}
}
c <- t
return err
func Send(c chan T, t T) (err error) {
defer func() {
if x := recover(); x != nil {
err = fmt.Errorf("Unable to send: %v", x)
}
}
c <- t
return
}
This is like writing
func Index(a []string, i int) (v string, ok bool) {
defer func() { recover() }()
v = a[i]
ok = true
return
}
Yes, it works, but it is an error-prone way to program.
It would be better not to send to closed channels.
Russ
Your way seems has a data race using -race, in production, you never meet a bug?