Skip to content

Commit 057b215

Browse files
committed
Tutorial9 - Channels
1 parent 6c2f1fb commit 057b215

File tree

3 files changed

+154
-1
lines changed

3 files changed

+154
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ go run tutorial1/main.go
4949
- [x] [Strings, Runes, and UTF-8 Encoding](tutorial5)
5050
- [x] [Structs and Interfaces](tutorial6)
5151
- [x] [Pointers](tutorial7)
52-
- [x] [Concurrency(Goroutines)](tutorial8)
52+
- [x] [Concurrency(Goroutines)](tutorial8)
53+
- [x] [Channels)](tutorial9)

tutorial9/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Go Channels
2+
3+
This Go program demonstrates the use of channels for communication between goroutines.
4+
5+
## Key Concepts
6+
7+
### Channels
8+
9+
Channels are a typed conduit through which you can send and receive values with the channel operator, `<-`.
10+
11+
```go
12+
var c = make(chan int) //this channel can only hold a single int value
13+
c <- 1 // adding value to this channel
14+
var i = <-c // retrieve the value from the channel now c will be empty and i = 1
15+
fmt.Println(i)
16+
```
17+
18+
If we run the above code directly, it will give a deadlock error. This is because the main goroutine is trying to receive a value from the channel, but there is no other goroutine available to send a value.
19+
20+
### Buffered Channels
21+
22+
Buffered channels can hold more than one value before they block. The capacity is passed as the second argument to `make`.
23+
24+
```go
25+
var c = make(chan int, 5) // this channel can hold up to 5 int values
26+
```
27+
28+
### Goroutines and Channels
29+
30+
Goroutines can send values into a channel, and other goroutines can receive those values.
31+
32+
```go
33+
func main() {
34+
var c = make(chan int, 5)
35+
go process2(c)
36+
fmt.Println(<-c)
37+
for i:= range c{
38+
fmt.Println(i)
39+
time.Sleep(time.Second+1)
40+
}
41+
}
42+
```
43+
44+
In the `main` function, we create a buffered channel `c` and spawn a goroutine `process2(c)`. We then receive a value from the channel and print it. We also range over the channel to receive all remaining values.
45+
46+
### Closing Channels
47+
48+
Senders can close a channel to indicate that no more values will be sent. Receivers can test whether a channel has been closed by assigning a second parameter to the receive expression.
49+
50+
```go
51+
func process2(c chan int){
52+
defer close(c)
53+
for i:=0; i<5; i++{
54+
c <- i
55+
}
56+
fmt.Println("Exiting Process")
57+
}
58+
```
59+
60+
In the `process2` function, we defer the call to `close(c)` to ensure that the channel will be closed once all values have been sent. We then send five values into the channel and print a message indicating that the process is exiting.
61+
62+
63+
### Checkout the code
64+
65+
- [main.go](main.go)

tutorial9/main.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"time"
7+
)
8+
9+
var MAX_CHICKEN_PRICE float32 = 5
10+
var MAX_TOFU_PRICE float32 = 3
11+
12+
func main(){
13+
var chickenChannel = make(chan string)
14+
var tofuChannel = make(chan string)
15+
var websites = []string{"swiggy", "zomato", "eatclub"}
16+
for i:= range websites{
17+
go checkChickenPrices(websites[i], chickenChannel)
18+
go checkTofuPrices(websites[i], tofuChannel)
19+
}
20+
sendMessage(chickenChannel, tofuChannel)
21+
}
22+
23+
func checkChickenPrices(website string, chickenChannel chan string){
24+
for {
25+
time.Sleep(time.Second+1)
26+
var chickenPrice = rand.Float32()*20
27+
if chickenPrice<=MAX_CHICKEN_PRICE{
28+
chickenChannel <- website
29+
break
30+
}
31+
}
32+
}
33+
34+
func checkTofuPrices(website string, tofuChannel chan string){
35+
for {
36+
time.Sleep(time.Second+1)
37+
var tofuPrice = rand.Float32()*20
38+
if tofuPrice<=MAX_TOFU_PRICE{
39+
tofuChannel <- website
40+
break
41+
}
42+
}
43+
}
44+
45+
func sendMessage(chickenChannel chan string, tofuChannel chan string){
46+
select{
47+
case website := <-chickenChannel:
48+
fmt.Printf("\nText Sent: Found deal on chicken at %v.", website)
49+
case website := <-tofuChannel:
50+
fmt.Printf("Email Sent: Found deal on tofu at %v.", website)
51+
}
52+
}
53+
54+
55+
56+
// func main() {
57+
// //var c = make(chan int)
58+
// var c = make(chan int, 5) //by adding this process function will run immediately but main function will take time
59+
// //go process(c)
60+
// go process2(c)
61+
// fmt.Println(<-c)
62+
// for i:= range c{
63+
// fmt.Println(i)
64+
// time.Sleep(time.Second+1)
65+
// }
66+
// }
67+
68+
// // func process(c chan int){
69+
// // c <- 123
70+
// // }
71+
72+
// func process2(c chan int){
73+
// defer close(c)
74+
// for i:=0; i<5; i++{
75+
// c <- i
76+
// }
77+
// fmt.Println("Exiting Process")
78+
// }
79+
80+
/*
81+
var c = make(chan int) //this channel can only hold a single int value
82+
c <- 1 // adding value to this channel
83+
var i = <-c // retrieve the value from the channel now c will be empty and i = 1
84+
fmt.Println(i)
85+
86+
if we run the above code direclty it will give deadlock error
87+
*/

0 commit comments

Comments
 (0)