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
| 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 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
| 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 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
| 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 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
| 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 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
| // 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")}, | |
| } |
NewerOlder