From 93cce988bbf4bb9067933071762b07edd1da55c2 Mon Sep 17 00:00:00 2001 From: "jian.han" Date: Fri, 19 Jan 2018 16:25:24 +1000 Subject: [PATCH] select channel with error handling --- concurrency/.DS_Store | Bin 0 -> 6148 bytes concurrency/goroutine_leak/main.go | 1 + concurrency/select_error_handling/main.go | 65 ++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 concurrency/.DS_Store create mode 100644 concurrency/select_error_handling/main.go diff --git a/concurrency/.DS_Store b/concurrency/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..acbcfefca556348b9b8c202905543b5a59b74efd GIT binary patch literal 6148 zcmeHK%Sr=55Ukc50$y?vJkBrp2TO=wkPnDrBnn{z@p0apzistH*{}#Xd64SRT{}Hp zGi)8U(*SJwzP$w&0OoW@e0rFg@4HXzqB2IL^NdHlVS^_e`8&^?dyQ9e_Vh9Tz%w>@ z#shk6x%)!geXNiD{?PC8#`CUFl@yQyQa}nw0Vyz2z0pQvfH-41jL$Jk5Su56UEz?(49${COsdt0VM%AcRb5v&BqkkJ!-v()RuhWF z?L5ClIjl=mlmb%VRDsJ}F1-KW(SMo$Pf1!y0V(jW6tLN9y;|~>s<%#F&U0}d&)n|{hs2;WA9SMr2)HgXDexBxd;ty-8EOCk literal 0 HcmV?d00001 diff --git a/concurrency/goroutine_leak/main.go b/concurrency/goroutine_leak/main.go index ad2e5c4..3109b03 100644 --- a/concurrency/goroutine_leak/main.go +++ b/concurrency/goroutine_leak/main.go @@ -20,6 +20,7 @@ goroutines in some sort of organized fashion. **/ func main() { + resourceLeak() cancellationSignal() } diff --git a/concurrency/select_error_handling/main.go b/concurrency/select_error_handling/main.go new file mode 100644 index 0000000..91c2886 --- /dev/null +++ b/concurrency/select_error_handling/main.go @@ -0,0 +1,65 @@ +package main + +import ( + "fmt" + "time" + + "github.com/davecgh/go-spew/spew" +) + +func main() { + if err := selectWithErrorDemo(); err != nil { + spew.Dump(err) + } + time.Sleep(time.Second * 5) +} + +func selectWithErrorDemo() error { + // start update race and market + count, errChan, doneChan := 2, make(chan error), make(chan bool) + + go func() { + // update race + if err := doFirstThing(); err != nil { + errChan <- err + } else { + doneChan <- true + fmt.Println("Finish First Go Routine") + } + + }() + + go func() { + // update market + if err := doSecondThing(); err != nil { + errChan <- err + } else { + doneChan <- true + fmt.Println("Finish Second Go Routine") + } + }() + + for i := 0; i < count; i++ { + select { + case err := <-errChan: + return err + case d := <-doneChan: + spew.Dump(i, d) + } + } + return nil +} + +func doFirstThing() error { + time.Sleep(time.Second * 2) + fmt.Println("Exectue Do First") + return nil + +} + +func doSecondThing() error { + time.Sleep(time.Second * 1) + fmt.Println("Exectue Do Second") + // return errors.New("Error In Second") + return nil +}