From a9779a3f8cb5298548fbb54e6d288d54a2aa1bc1 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 13 Nov 2022 14:55:38 -0600 Subject: [PATCH] more examples --- example-go-channel/Makefile | 5 +++ example-go-channel/main.go | 1 + example-go-channel/return-vals.go | 52 +++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 example-go-channel/return-vals.go diff --git a/example-go-channel/Makefile b/example-go-channel/Makefile index eea6cea..c35c838 100644 --- a/example-go-channel/Makefile +++ b/example-go-channel/Makefile @@ -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 diff --git a/example-go-channel/main.go b/example-go-channel/main.go index a986987..cdb25a3 100644 --- a/example-go-channel/main.go +++ b/example-go-channel/main.go @@ -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() diff --git a/example-go-channel/return-vals.go b/example-go-channel/return-vals.go new file mode 100644 index 0000000..307d2ef --- /dev/null +++ b/example-go-channel/return-vals.go @@ -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) + } + */ +}