more examples
This commit is contained in:
parent
15efa4258e
commit
a9779a3f8c
|
@ -4,3 +4,8 @@ build:
|
|||
|
||||
simple:
|
||||
GO111MODULE="off" go build -v -x -o ~/go/bin/simple-worker simple-worker.go
|
||||
simple-worker
|
||||
|
||||
return-vals:
|
||||
GO111MODULE="off" go build -v -x -o ~/go/bin/return-vals return-vals.go
|
||||
return-vals
|
||||
|
|
|
@ -14,6 +14,7 @@ func generateNumbers(total int, ch chan<- int, wg *sync.WaitGroup) {
|
|||
for idx := 1; idx <= total; idx++ {
|
||||
fmt.Printf("START sending %d to channel\n", idx)
|
||||
ch <- idx
|
||||
// res, err := (<-r)()
|
||||
fmt.Printf("END sending %d to channel\n", idx)
|
||||
}
|
||||
wg.Wait()
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
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)
|
||||
}
|
||||
*/
|
||||
}
|
Loading…
Reference in New Issue