Skip to content

Commit b106b8d

Browse files
committed
Added go-routines and select
0 parents  commit b106b8d

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
# Go Concurrency Patterns
3+
4+
Concurrency Patterns about go are collected in this repo. For **learning purposes**
5+
6+
7+
## Appendix
8+
9+
Concurrency refers to the ability of a software system to handle **multiple tasks** or **processes at the same time**. It allows programs to execute several computations simultaneously, making efficient use of available resources.
10+
11+
In software, concurrency doesn’t necessarily mean true simultaneous execution on multiple cores. Instead, it involves managing multiple tasks by **interleaving their execution**, giving the **illusion of parallelism**. This is typically achieved through mechanisms like threading, asynchronous programming, or the use of concurrent constructs like **goroutines** in Go or **async/await** in languages like JavaScript.
12+
13+
Concurrency **enables applications to remain responsive by managing various operations concurrently**, such as handling user input, processing data, or performing I/O operations, without waiting for one task to finish before starting another. It's essential in designing systems that can efficiently utilize available resources and improve overall performance.
14+
15+
16+
## Lessons Learned
17+
18+
#### Goroutine
19+
A goroutine is a lightweight thread of execution in Go. It's a concurrent unit of work that can be executed independently within a Go program. **Goroutines are managed by the Go runtime** and allow multiple functions to run concurrently. They're different from threads in other programming languages in that they're managed at a higher level by the Go runtime, enabling efficient scheduling and utilization of resources. Goroutines are relatively cheap to create and are fundamental to building **concurrent and scalable applications** in Go.
20+
21+
22+
#### Select
23+
The select statement is used to listen for data coming from multiple channels concurrently, but the crucial aspect is the concurrent (or asynchronous) nature. Especially when multiple tasks are being performed, using the select statement allows simultaneous access to data from channels and optimizes the processing time of the program.
24+
25+
For instance, when waiting for data from the network or different threads of execution, it enables the main thread to continue doing other tasks while waiting for data from channels. **Instead of waiting idly until data arrives** from any channel, **select concurrently monitors multiple channels** and acts accordingly when data arrives.
26+
27+
**The main thread waits until data is received from** any of the channels being listened to with the select statement. However, it can also continue with other tasks, execute other threads of execution, or perform different operations.
28+
29+
This feature allows programs to monitor and manage various tasks concurrently, preventing the program from blocking in unexpected situations. The use of select is highly beneficial in scenarios involving asynchronous operations or listening for data from various sources.
30+
31+
#### Buffered vs Unbuffered Channels
32+
#### The Done Channel
33+
#### Pipeline
34+
35+
36+
37+

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/erdemkosk/go-concurrency
2+
3+
go 1.21.4

goroutines.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
// A goroutine is a lightweight thread of execution in Go. It's a concurrent unit of work that can be executed independently within a Go program.
9+
// Goroutines are managed by the Go runtime and allow multiple functions to run concurrently.
10+
// They're different from threads in other programming languages in that they're managed at a higher level by the Go runtime, enabling efficient scheduling and utilization of resources.
11+
// Goroutines are relatively cheap to create and are fundamental to building concurrent and scalable applications in Go.
12+
13+
func GoRoutinesExample() {
14+
fmt.Println("--------------------------")
15+
channel1 := make(chan string)
16+
channel2 := make(chan string)
17+
18+
go goRoutine1(channel1)
19+
go goRoutine2(channel2)
20+
21+
msg1 := <-channel1
22+
msg2 := <-channel2
23+
24+
fmt.Println(msg1)
25+
fmt.Println(msg2)
26+
27+
fmt.Println("Hello from GoRoutines!")
28+
}
29+
30+
func goRoutine1(c chan string) {
31+
time.Sleep(1 * time.Second)
32+
c <- "1"
33+
}
34+
35+
func goRoutine2(c chan string) {
36+
time.Sleep(1 * time.Second)
37+
c <- "2"
38+
}

main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package main
2+
3+
func main() {
4+
GoRoutinesExample()
5+
SelectExample()
6+
}

select.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
// The select statement is used to listen for data coming from multiple channels concurrently, but the crucial aspect is the concurrent (or asynchronous) nature.
9+
// Especially when multiple tasks are being performed, using the select statement allows simultaneous access to data from channels and optimizes the processing time of the program.
10+
11+
// For instance, when waiting for data from the network or different threads of execution, it enables the main thread to continue doing other tasks while waiting for data from channels.
12+
// Instead of waiting idly until data arrives from any channel, select concurrently monitors multiple channels and acts accordingly when data arrives.
13+
14+
// The main thread waits until data is received from any of the channels being listened to with the select statement. However, it can also continue with other tasks,
15+
// execute other threads of execution, or perform different operations.
16+
17+
// This feature allows programs to monitor and manage various tasks concurrently, preventing the program from blocking in unexpected situations.
18+
// The use of select is highly beneficial in scenarios involving asynchronous operations or listening for data from various sources.
19+
20+
func SelectExample() {
21+
fmt.Println("--------------------------")
22+
channel3 := make(chan string)
23+
channel4 := make(chan string)
24+
25+
go goRoutine3(channel3)
26+
go goRoutine4(channel4)
27+
28+
select {
29+
case msg1 := <-channel3:
30+
fmt.Println("Received from Channel 3:", msg1)
31+
case msg2 := <-channel4:
32+
fmt.Println("Received from Channel 4:", msg2)
33+
}
34+
35+
fmt.Println("Hello From Select!")
36+
}
37+
38+
func goRoutine3(c chan string) {
39+
time.Sleep(1 * time.Second)
40+
c <- "3"
41+
}
42+
43+
func goRoutine4(c chan string) {
44+
time.Sleep(1 * time.Second)
45+
c <- "4"
46+
}

0 commit comments

Comments
 (0)