February 2018
Intermediate to advanced
340 pages
9h 43m
English
package main import ( "context" "fmt" "sync" "time" ) type SearchSrc struct { ID string Delay int } func (s *SearchSrc) Search(ctx context.Context) <-chan string { out := make(chan string) go func() { time.Sleep(time.Duration(s.Delay) * time.Second) select { case out <- "Result " + s.ID: case <-ctx.Done(): fmt.Println("Search received Done()") } close(out) fmt.Println("Search finished for ID: " + s.ID) }() return out } func main() { ctx, cancel := context.WithCancel(context.Background()) src1 := &SearchSrc{"1", 2} src2 := &SearchSrc{"2", 6} r1 := src1.Search(ctx) r2 := src2.Search(ctx)Read now
Unlock full access