golang-examples/example-go-channel/return-vals.go

53 lines
808 B
Go
Raw Permalink Normal View History

2022-11-13 14:55:38 -06:00
package main
import (
"log"
)
var res int
var err error
type FuncResult func() (int, error)
/*
func funcWithError(f chan FuncResult) {
f <- (func() (int, error) { return 123, nil })
}
func main() {
r := make(chan FuncResult)
//...
}
*/
func funcWithError(f chan FuncResult) {
f <- (func() (int, error) { return 123, nil })
}
func blah() (int, error) {
log.Println("Ran blah()")
return 21, nil
}
func main() {
// r := make(chan func() (int, error))
r := make(chan FuncResult)
go funcWithError(r)
res, err = (<-r)()
if err == nil {
log.Printf("My result is %d again!", res)
} else {
log.Printf("The func returned an error: %s", err)
}
/*
r <= blah
if err == nil {
log.Printf("My result is %d again!", res)
} else {
log.Printf("The func returned an error: %s", err)
}
*/
}