more examples

This commit is contained in:
Jeff Carr 2022-11-13 14:55:38 -06:00
parent 15efa4258e
commit a9779a3f8c
3 changed files with 58 additions and 0 deletions

View File

@ -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

View File

@ -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()

View File

@ -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)
}
*/
}