diff --git a/concurrency/basic_channel_error_handling/main.go b/concurrency/basic_channel_error_handling/main.go new file mode 100644 index 0000000..29523b4 --- /dev/null +++ b/concurrency/basic_channel_error_handling/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "math/rand" + "time" +) + +func fetchAll() error { + var N = 4 + quit := make(chan bool) + errc := make(chan error) + done := make(chan error) + for i := 0; i < N; i++ { + go func(i int) { + // dummy fetch + time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond) + err := error(nil) + if rand.Intn(2) == 0 { + err = fmt.Errorf("goroutine %d's error returned", i) + } + ch := done // we'll send to done if nil error and to errc otherwise + if err != nil { + ch = errc + } + select { + case ch <- err: + return + case <-quit: + return + } + }(i) + } + count := 0 + for { + select { + case err := <-errc: + close(quit) + return err + case <-done: + count++ + if count == N { + return nil // got all N signals, so there was no error + } + } + } +} + +func main() { + rand.Seed(time.Now().UnixNano()) + fmt.Println(fetchAll()) +} diff --git a/idiom/iota_const/main.go b/idiom/iota_const/main.go new file mode 100644 index 0000000..6f94d86 --- /dev/null +++ b/idiom/iota_const/main.go @@ -0,0 +1,22 @@ +package main + +import "github.com/davecgh/go-spew/spew" + +type FeedLockLinkType int + +const ( + Racing FeedLockLinkType = iota + Market +) + +var feedLockLinkTypes = [...]string{ + "racing.Races.raceID", + "racing.Markets.marketID", +} + +func (f FeedLockLinkType) String() string { return feedLockLinkTypes[f] } + +func main() { + test := FeedLockLinkType(Racing) + spew.Dump(test) +}