Skip to content

Commit d98d7b2

Browse files
committed
Increased scope of project to full mastery of Golang
1 parent b7ab5cc commit d98d7b2

File tree

7 files changed

+79
-2
lines changed

7 files changed

+79
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# golang-concurrency
1+
# mastering-golang
22

3-
mastering concurrency in Go using various resources.
3+
Mastering Golang, especially concurrencym using various resources.

concurrency/buff_chan/buff_chan.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
ch := make(chan int, 2)
7+
ch <- 1
8+
ch <- 2
9+
10+
fmt.Println(<-ch)
11+
fmt.Println(<-ch)
12+
13+
// this would cause a deadlock since the call is blocking,
14+
// and nothing is going to ever fill that channel with a value
15+
//fmt.Println(<-ch)
16+
17+
// this is fine though since we do fill in the channel's queue with
18+
// a value using the goroutine
19+
20+
go func() {
21+
ch <- 3
22+
}()
23+
24+
fmt.Println(<-ch)
25+
}

concurrency/buff_chan/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module conc/buff_chan
2+
3+
go 1.18
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
nums := []int{1, 2, 3, 4, 5}
7+
8+
ch := make(chan int)
9+
go sum(nums[:len(nums)/2], ch)
10+
go sum(nums[len(nums)/2:], ch)
11+
12+
total := <-ch + <-ch
13+
fmt.Printf("Total = %d\n", total)
14+
15+
}
16+
17+
func sum(nums []int, ch chan<- int) {
18+
sum := 0
19+
for _, n := range nums {
20+
sum += n
21+
}
22+
23+
ch <- sum
24+
}

concurrency/channel_sum/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module conc/channel_sum
2+
3+
go 1.18

concurrency/hello/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module conc/hello
2+
3+
go 1.18

concurrency/hello/hello.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
func main() {
9+
go say("world")
10+
say("hello")
11+
12+
}
13+
14+
func say(s string) {
15+
for i := 0; i < 5; i++ {
16+
time.Sleep(100 * time.Millisecond)
17+
fmt.Println(s)
18+
}
19+
}

0 commit comments

Comments
 (0)