forked from andcloudio/go-concurrency-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
69 changed files
with
440 additions
and
397 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.idea | ||
.DS_Store |
File renamed without changes.
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
13 changes: 2 additions & 11 deletions
13
...tro/02-join/03-add/counting/count_test.go → ...-goroutines/04-add/counting/count_test.go
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/andcloudio/go-concurrency-exercises/01-intro/01-goroutines/04-add/counting" | ||
) | ||
|
||
func main() { | ||
numbers := counting.GenerateNumbers(1e7) | ||
|
||
t := time.Now() | ||
_ = counting.Add(numbers) | ||
fmt.Printf("Sequential Add took: %s\n", time.Since(t)) | ||
|
||
t = time.Now() | ||
_ = counting.AddConcurrent(numbers) | ||
fmt.Printf("Concurrent Add took: %s\n", time.Since(t)) | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
ch := make(chan int) | ||
|
||
go func(a, b int) { | ||
c := a + b | ||
ch <- c | ||
}(1, 2) | ||
// get the value computed from goroutine | ||
c := <-ch | ||
fmt.Printf("computed value %v\n", c) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
ch := make(chan int) | ||
go func() { | ||
for i := 0; i < 6; i++ { | ||
// send iterator over channel | ||
ch <- i | ||
} | ||
close(ch) | ||
}() | ||
// range over channel to recv values | ||
for i := range ch { | ||
fmt.Println(i) | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
// TODO: Implement ping pong with Channel Direction | ||
|
||
func ping(out chan<- string) { | ||
// send message on ch1 | ||
out <- "ping" | ||
} | ||
|
||
func pong(in <-chan string, out chan<- string) { | ||
// recv message on ch1 | ||
msg := <-in | ||
msg = msg + " pong" | ||
// send it on ch2 | ||
out <- msg | ||
} | ||
|
||
func main() { | ||
// create ch1 and ch2 | ||
ch1 := make(chan string) | ||
ch2 := make(chan string) | ||
|
||
// spine goroutine ping and pong | ||
go ping(ch1) | ||
go pong(ch1, ch2) | ||
|
||
// recv message on ch2 | ||
msg := <-ch2 | ||
|
||
fmt.Println(msg) | ||
} |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func main() { | ||
ch := make(chan string, 1) | ||
|
||
go func() { | ||
time.Sleep(2 * time.Second) | ||
ch <- "one" | ||
}() | ||
|
||
//implement timeout for recv on channel ch | ||
select { | ||
case m := <-ch: | ||
fmt.Println(m) | ||
case <-time.After(1 * time.Second): | ||
fmt.Println("timeout") | ||
} | ||
} |
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
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
Oops, something went wrong.