This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "log" | |
| ) | |
| type logger struct{} | |
| func (l *logger) do(in string) { | |
| log.Println(in) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type doer interface { | |
| do(string) | |
| } | |
| type validator interface { | |
| validate(string) bool | |
| do(int) | |
| } | |
| type doerAndValidator interface { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type doer interface { | |
| do(string) | |
| } | |
| type validator interface { | |
| validate(string) bool | |
| do(string) | |
| } | |
| type doerAndValidator interface { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "log" | |
| ) | |
| type doer interface { | |
| do(string) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| "net/http" | |
| "github.com/cenkalti/backoff/v4" | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| timeout := time.Millisecond * 500 | |
| timeoutTimer := time.NewTimer(timeout) | |
| defer timeoutTimer.Stop() | |
| eg.Go(func() error { | |
| for { | |
| select { | |
| case <-done(): | |
| fmt.Println("Sender: Context closed") | |
| return ctx.Err() | |
| case <-timeoutTimer.C: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| done := ctx.Done() | |
| eg, ctx := errgroup.WithContext(ctx) | |
| eg.Go(func() error { | |
| defer close(lines) | |
| for { | |
| if !scanner.Scan() { | |
| fmt.Println("Reader: Completed") | |
| break | |
| } | |
| lines <- scanner.Text() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| eg.Go(func() error { | |
| for l := range lines { | |
| select { | |
| case <-ctx.Done(): | |
| fmt.Println("Sender: Context closed") | |
| return ctx.Err() | |
| default: | |
| fmt.Printf("Sender: Sending %s to remote database\n", l) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| eg.Go(func() error { | |
| defer close(lines) | |
| for { | |
| if !scanner.Scan() { | |
| fmt.Println("Reader: Completed") | |
| break | |
| } | |
| lines <- scanner.Text() | |
| time.Sleep(time.Second) // To fake the slow writing to the lines channel |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| eg, ctx := errgroup.WithContext(ctx) | |
| eg.Go(func() error { | |
| defer close(lines) | |
| for { | |
| if !scanner.Scan() { | |
| fmt.Println("Reader: Completed") | |
| break | |
| } | |
| lines <- scanner.Text() |