Skip to content

Commit 172f5b0

Browse files
adiciona diretório de concurrency
1 parent 6a190a5 commit 172f5b0

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

concurrency/channels.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

concurrency/goroutines.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

concurrency/select.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

0 commit comments

Comments
 (0)