Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
149 lines (119 loc) · 4.56 KB
/
Copy pathmain.go
File metadata and controls
149 lines (119 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// package main
// import "fmt"
// func main() {
// messagesChannel := make(chan string) // Create a channel of type string
// messagesChannel <- "Hello, World!" // Send a message to the channel
// message := <-messagesChannel // Receive a message from the channel
// fmt.Println(message) // Print the received message
// }
// In this example, we create a channel of type string using make(chan string). We then send a message "Hello, World!" to the channel using the <- operator. Finally, we receive the message from the channel and print it to the console. Channels are a powerful feature in Go that allow for communication and synchronization between goroutines. The program will deadlock because the main goroutine is trying to send a message to the channel before any goroutine is ready to receive it. To avoid this, you can use a separate goroutine to send the message or use buffered channels.
/*
package main
import (
"fmt"
"time"
)
// Example of using a separate goroutine to send a message to the channel
func processMessage(messagesChannel chan string) {
for msg := range messagesChannel {
fmt.Println(msg)
}
}
// Example of using a channel to calculate the sum of numbers in a separate goroutine
func sum(numbers []int, resultsChannel chan int) {
sum := 0
for _, n := range numbers {
sum += n
}
resultsChannel <- sum
}
// Example of using a channel to signal when a task is done
func task(done chan bool) {
fmt.Println("Task starting...")
defer func() {
done <- true
}()
}
// Example of using a channel to send emails in a separate goroutine
func emailSender(emailChannel chan string, done chan bool) {
defer func() {
done <- true
}()
for email := range emailChannel {
fmt.Printf("Sending email to %s\n", email)
time.Sleep(time.Millisecond * 500) // Simulate time taken to send an email
}
done <- true
}
func main() {
chan1 := make(chan int)
chan2 := make(chan string)
// Start a separate goroutine to send a message to chan1
go func() {
chan1 <- 42
}()
// Start a separate goroutine to send a message to chan2
go func() {
chan2 <- "Hello, World!"
}()
// Receive messages from chan1 and chan2
for i := 0; i < 2; i++ {
select {
case num := <-chan1:
fmt.Printf("Received from chan1: %d\n", num)
case msg := <-chan2:
fmt.Printf("Received from chan2: %s\n", msg)
}
}
// Example of using a channel to send emails in a separate goroutine
emailChannel := make(chan string, 100) // Create a channel of type string
done := make(chan bool) // Create a channel of type bool
for i := 1; i <= 5; i++ {
emailChannel <- fmt.Sprintf("test%d@example.com", i)
}
close(emailChannel)
go emailSender(emailChannel, done) // Start a goroutine to send emails
fmt.Println("Emails queued for sending...")
<-done // Wait for the email sender to finish
// Example of using a channel to signal when a task is done
done = make(chan bool) // Create a channel of type bool
go task(done) // Start a goroutine to run the task
<-done // Wait for the task to complete
// Example of using a channel to calculate the sum of numbers in a separate goroutine
resultsChannel := make(chan int) // Create a channel of type int
go sum([]int{1, 2, 3, 4, 5}, resultsChannel) // Start a goroutine to calculate the sum
result := <-resultsChannel // Receive the result from the channel
fmt.Println("Sum:", result)
// Example of using a separate goroutine to send a message to the channel
messagesChannel := make(chan string) // Create a channel of type string
go processMessage(messagesChannel)
for {
messagesChannel <- "Hello, World!" // Send a message to the channel
time.Sleep(time.Second) // Sleep for a second to allow the goroutine to finish
}
// time.Sleep(time.Second) // Sleep for a second to allow the goroutine to finish
// messagesChannel <- "Hello, World!" // Send a message to the channel
}
*/
package main
import (
"fmt"
"time"
)
type User struct {
ID int
Name string
}
func main() {
ch := make(chan User) // Create a channel of type User, unbuffered channel
go func() {
time.Sleep(200 * time.Millisecond) // Simulate some processing time
fmt.Println("Sending user to channel...")
// blocking send operation, will wait until the main goroutine receives the value
ch <- User{ID: 1, Name: "John"} // Send a User struct to the channel
fmt.Println("User sent to channel.")
}()
fmt.Println("Waiting to receive user from channel...")
user := <-ch // blocking receive operation, will wait until the goroutine sends the value
fmt.Printf("Received user: %+v\n", user)
}