From 17a2b6b98e56a350c51eab5ea1e0a2aa8fc4709c Mon Sep 17 00:00:00 2001 From: GoesToEleven Date: Fri, 20 Jan 2017 09:49:46 -0800 Subject: [PATCH 1/7] you're doing great --- .../{ => 01_original-solution}/main.go | 0 .../02_another-solution/main.go | 45 +++++++++++++++++++ .../04_challenge-solution/README.md | 42 +++++++++++++++++ 3 files changed, 87 insertions(+) rename 22_go-routines/12_channels_pipeline/04_challenge-solution/{ => 01_original-solution}/main.go (100%) create mode 100644 22_go-routines/12_channels_pipeline/04_challenge-solution/02_another-solution/main.go create mode 100644 22_go-routines/12_channels_pipeline/04_challenge-solution/README.md diff --git a/22_go-routines/12_channels_pipeline/04_challenge-solution/main.go b/22_go-routines/12_channels_pipeline/04_challenge-solution/01_original-solution/main.go similarity index 100% rename from 22_go-routines/12_channels_pipeline/04_challenge-solution/main.go rename to 22_go-routines/12_channels_pipeline/04_challenge-solution/01_original-solution/main.go diff --git a/22_go-routines/12_channels_pipeline/04_challenge-solution/02_another-solution/main.go b/22_go-routines/12_channels_pipeline/04_challenge-solution/02_another-solution/main.go new file mode 100644 index 00000000..2988974a --- /dev/null +++ b/22_go-routines/12_channels_pipeline/04_challenge-solution/02_another-solution/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "fmt" + "math/rand" + "sync" +) + +const numFactorials = 100 +const rdLimit = 20 + +func main() { + var w sync.WaitGroup + w.Add(numFactorials) + factorial(numFactorials, &w) + w.Wait() + +} + +func factorial(n int, wmain *sync.WaitGroup) { + var w sync.WaitGroup + rand.Seed(42) + + w.Add(n + 1) + + for j := 1; j <= n; j++ { + + go func() { + f := rand.Intn(rdLimit) + w.Wait() + total := 1 + for i := f; i > 0; i-- { + + total *= i + } + fmt.Println(f, total) + (*wmain).Done() + //out <- total + + }() + w.Done() + } + fmt.Println("All done with initialization") + w.Done() +} \ No newline at end of file diff --git a/22_go-routines/12_channels_pipeline/04_challenge-solution/README.md b/22_go-routines/12_channels_pipeline/04_challenge-solution/README.md new file mode 100644 index 00000000..8589eeda --- /dev/null +++ b/22_go-routines/12_channels_pipeline/04_challenge-solution/README.md @@ -0,0 +1,42 @@ +A student sent me the below note so I included the student's excellent solution to this project: + +Todd, I reviewed your solution to 22_go-routines/04_challenge-solution in your Golang Programming class. + +I believe your solution might not run 100 factorial computations concurrently and in parallel as the following statement will calculate the factorials sequentially, since it is receiving them - one by one - from the pipeline: +out <- fact(n) + +I used the sync package to create a solution that I believe will accomplish your goal. Let me know if I am missing something: + +https://github.com/arsanjea/udemyTraining/blob/master/Exercises/exercise38.go + +///// + +# Definitions + +## concurrency +a design pattern +go uses goroutines to create concurrent design patterns + +## parallelism +running code from a program on more than one cpu +parallelism implies you have used concurrent design patterns + +## sequentially +one thing happening after another, in sequence + +# here are my thoughts + +The original solution uses concurrent design patterns. It uses two different goroutines. The [control flow](https://en.wikipedia.org/wiki/Control_flow) of the program is no longer a straight top-to-bottom sequence. Different goroutines have been launched. + +The program may or may not run in parallel. If the program is run on a machine with two or more cpus, the program has the potential to run in parallel. Each of our three goroutines (the two goroutines we launched, and main) could be running on different cpu cores. + +Jean-Marc Arsan is correct: the program IS still running sequentially. Even though calculations are occuring in different goroutines, and potentially on different CPU cores, the sequence in which they occur is still sequential. + +goroutines allow synchronization of code. + +In this original example, the code is synchronized. + +Thank you, Jean-Marc, for your comment on this code! + +I appreciate these discussions and hope the notes and your new code sample are helpful to everyone! + From e67e348bb8a393897272ad1f58fb81edca11d149 Mon Sep 17 00:00:00 2001 From: GoesToEleven Date: Wed, 1 Feb 2017 06:44:54 -0800 Subject: [PATCH 2/7] you're doing great --- 22_go-routines/08_atomicity/main.go | 2 +- .../04_challenge-solution/02_another-solution/main.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/22_go-routines/08_atomicity/main.go b/22_go-routines/08_atomicity/main.go index c2ae26a5..bac97ab9 100644 --- a/22_go-routines/08_atomicity/main.go +++ b/22_go-routines/08_atomicity/main.go @@ -23,7 +23,7 @@ func incrementor(s string) { for i := 0; i < 20; i++ { time.Sleep(time.Duration(rand.Intn(3)) * time.Millisecond) atomic.AddInt64(&counter, 1) - fmt.Println(s, i, "Counter:", counter) + fmt.Println(s, i, "Counter:", atomic.LoadInt64(&counter)) } wg.Done() } diff --git a/22_go-routines/12_channels_pipeline/04_challenge-solution/02_another-solution/main.go b/22_go-routines/12_channels_pipeline/04_challenge-solution/02_another-solution/main.go index 2988974a..c9a7592e 100644 --- a/22_go-routines/12_channels_pipeline/04_challenge-solution/02_another-solution/main.go +++ b/22_go-routines/12_channels_pipeline/04_challenge-solution/02_another-solution/main.go @@ -12,18 +12,18 @@ const rdLimit = 20 func main() { var w sync.WaitGroup w.Add(numFactorials) - factorial(numFactorials, &w) + factorial(&w) w.Wait() } -func factorial(n int, wmain *sync.WaitGroup) { +func factorial(wmain *sync.WaitGroup) { var w sync.WaitGroup rand.Seed(42) - w.Add(n + 1) + w.Add(numFactorials + 1) - for j := 1; j <= n; j++ { + for j := 1; j <= numFactorials; j++ { go func() { f := rand.Intn(rdLimit) From 3b3aaa243d72bd0e7bd11d25c77a811ac635dcd5 Mon Sep 17 00:00:00 2001 From: GoesToEleven Date: Wed, 1 Feb 2017 06:45:47 -0800 Subject: [PATCH 3/7] you're doing great --- 22_go-routines/08_atomicity/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/22_go-routines/08_atomicity/main.go b/22_go-routines/08_atomicity/main.go index bac97ab9..bf7211c1 100644 --- a/22_go-routines/08_atomicity/main.go +++ b/22_go-routines/08_atomicity/main.go @@ -23,7 +23,7 @@ func incrementor(s string) { for i := 0; i < 20; i++ { time.Sleep(time.Duration(rand.Intn(3)) * time.Millisecond) atomic.AddInt64(&counter, 1) - fmt.Println(s, i, "Counter:", atomic.LoadInt64(&counter)) + fmt.Println(s, i, "Counter:", atomic.LoadInt64(&counter)) // access without race } wg.Done() } From 6d2d73139c5ee8b929b64ed8941a84fbead8c640 Mon Sep 17 00:00:00 2001 From: GoesToEleven Date: Fri, 10 Feb 2017 22:41:22 -0800 Subject: [PATCH 4/7] you're doing great --- 22_go-routines/15_for-fun/01/main.go | 40 ++++++++++++++++++++++++++++ 22_go-routines/15_for-fun/README.md | 5 ++++ 2 files changed, 45 insertions(+) create mode 100644 22_go-routines/15_for-fun/01/main.go create mode 100644 22_go-routines/15_for-fun/README.md diff --git a/22_go-routines/15_for-fun/01/main.go b/22_go-routines/15_for-fun/01/main.go new file mode 100644 index 00000000..b8542f49 --- /dev/null +++ b/22_go-routines/15_for-fun/01/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" +) + +func main() { + m := map[int]int{} + m[4] = 7 + m[3] = 87 + m[72] = 19 + + ch := make(chan int) + + // for closing ch + ch2 := make(chan int) + go func() { + var i int + for n := range ch2 { + i += n + if i == len(m) { + close(ch) + } + } + }() + + go func() { + for _, v := range m { + ch <- v + ch2 <- 1 + } + }() + + for v := range ch { + fmt.Println(v) + } + + // good housekeeping + close(ch2) +} \ No newline at end of file diff --git a/22_go-routines/15_for-fun/README.md b/22_go-routines/15_for-fun/README.md new file mode 100644 index 00000000..8e256c9b --- /dev/null +++ b/22_go-routines/15_for-fun/README.md @@ -0,0 +1,5 @@ +# New Code + +These files were added into this repo after the training was recorded. + +There are no lectures associated with these files. However, please peruse them and enjoy them! \ No newline at end of file From 9e2be86862cd4243a87ce10a9cf4bd8e4d1f963a Mon Sep 17 00:00:00 2001 From: Spock McLeod Date: Wed, 2 Aug 2017 16:04:05 -0700 Subject: [PATCH 5/7] you're doing great --- LICENSE.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE.txt b/LICENSE.txt index 300374b8..348cf030 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,2 +1,2 @@ All material is licensed under the Apache License Version 2.0, January 2004 -http://www.apache.org/licenses/LICENSE-2.0 \ No newline at end of file +http://www.apache.org/licenses/LICENSE-2.0 From b3380c63b1d19b9b47f8516d5bf6b08555c477ab Mon Sep 17 00:00:00 2001 From: Todd McLeod Date: Tue, 4 Dec 2018 15:42:05 -0800 Subject: [PATCH 6/7] you're doing great --- 20_struct/00_object-oriented/notes.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/20_struct/00_object-oriented/notes.txt b/20_struct/00_object-oriented/notes.txt index e4ff4ffb..91aade74 100644 --- a/20_struct/00_object-oriented/notes.txt +++ b/20_struct/00_object-oriented/notes.txt @@ -23,7 +23,7 @@ Classes -- classes hold both: ==== state / data / fields ==== behavior / methods --- Public / private +-- public / private Inheritence From efe77870b605c9c0bc85f1850267dcef22d32fcb Mon Sep 17 00:00:00 2001 From: Lebedev Date: Sun, 31 May 2020 13:26:35 +0300 Subject: [PATCH 7/7] typo fixed --- 20_struct/00_object-oriented/notes.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/20_struct/00_object-oriented/notes.txt b/20_struct/00_object-oriented/notes.txt index 91aade74..e62344cb 100644 --- a/20_struct/00_object-oriented/notes.txt +++ b/20_struct/00_object-oriented/notes.txt @@ -6,7 +6,7 @@ behavior ("methods") exported / un-exported (2) Reusability -inheritence ("embedded types") +inheritance ("embedded types") (3) Polymorphism interfaces @@ -25,7 +25,7 @@ Classes ==== behavior / methods -- public / private -Inheritence +Inheritance ////////////// In Go: