File tree 3 files changed +67
-0
lines changed
3 files changed +67
-0
lines changed Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import "fmt"
4
+
5
+ func producer (ch chan string ) {
6
+ ch <- "Message from Producer"
7
+ close (ch )
8
+ }
9
+
10
+ func main () {
11
+ ch := make (chan string )
12
+ go producer (ch )
13
+
14
+ for msg := range ch {
15
+ fmt .Println (msg )
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ // Praticando o paralelismo com go.
2
+
3
+ package main
4
+
5
+ import (
6
+ "fmt"
7
+ "time"
8
+ )
9
+
10
+ func printMessage (msg string ) {
11
+ for i := 0 ; i < 5 ; i ++ {
12
+ fmt .Println (msg )
13
+ time .Sleep (500 * time .Millisecond )
14
+ }
15
+ }
16
+
17
+ func main () {
18
+ go printMessage ("Hello from Goroutine!" )
19
+ printMessage ("Hello from Main!" )
20
+ }
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ "time"
6
+ )
7
+
8
+ func main () {
9
+ ch1 := make (chan string )
10
+ ch2 := make (chan string )
11
+
12
+ go func () {
13
+ time .Sleep (1 * time .Second )
14
+ ch1 <- "Message from Channel 1"
15
+ }()
16
+
17
+ go func () {
18
+ time .Sleep (2 * time .Second )
19
+ ch2 <- "Message from Channel 2"
20
+ }()
21
+
22
+ for i := 0 ; i < 2 ; i ++ {
23
+ select {
24
+ case msg1 := <- ch1 :
25
+ fmt .Println (msg1 )
26
+ case msg2 := <- ch2 :
27
+ fmt .Println (msg2 )
28
+ }
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments