This file contains 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
// Step 1: Write a failing test | |
func TestURLShortner(t *testing.T) { | |
var tests = []struct { | |
name, in, expected string | |
err error | |
}{ | |
{"NoScheme", "https://google.com", "1", nil}, | |
{"NoScheme", "google", "", errors.New("invalid")}, | |
} |
This file contains 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
lines := make(chan string, 1) | |
go func() { | |
defer close(lines) | |
for { | |
if !scanner.Scan() { | |
fmt.Println("Reader: Completed") | |
break | |
} | |
lines <- scanner.Text() |
This file contains 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
ctx, cancel := context.WithCancel(context.Background()) | |
sigs := make(chan os.Signal, 1) | |
signal.Notify(sigs, syscall.SIGTERM) | |
go func() { | |
sig := <-sigs | |
fmt.Printf("Caught %s\n", sig) | |
cancel() | |
}() |
This file contains 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
lines := make(chan string, 1) | |
go func() { | |
defer close(lines) | |
for { | |
if !scanner.Scan() { | |
fmt.Println("Reader: Completed") | |
break | |
} | |
lines <- scanner.Text() |
This file contains 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
var wg sync.WaitGroup | |
wg.Add(1) | |
go func() { | |
defer func() { | |
wg.Done() | |
close(lines) | |
}() | |
for { | |
if !scanner.Scan() { | |
fmt.Println("Reader: Completed") |
This file contains 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() |
This file contains 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 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 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 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: |
OlderNewer