G
Concorrência (Goroutines & Canais)
Go Syntax Guide
Lidar com operações concorrentes com goroutines e canais
Concorrência (Goroutines & Canais)
Lidar com operações concorrentes com goroutines e canais
Go concorrência (goroutines & canais) (go)
package main
import (
"fmt"
"sync"
"time"
)
// Function to run as goroutine
func printNumbers(id int) {
for i := 0; i < 5; i++ {
fmt.Printf("Goroutine %d: %d\n", id, i)
time.Sleep(100 * time.Millisecond)
}
}
// Function that returns a channel
func count(to int) <-chan int {
ch := make(chan int)
go func() {
for i := 1; i <= to; i++ {
ch <- i
time.Sleep(500 * time.Millisecond)
}
close(ch)
}()
return ch
}
// Producer-consumer pattern
func producer(ch chan<- int, wg *sync.WaitGroup) {
defer wg.Done()
for i := 0; i < 5; i++ {
ch <- i
fmt.Println("Produced:", i)
time.Sleep(100 * time.Millisecond)
}
close(ch)
}
func consumer(ch <-chan int, wg *sync.WaitGroup) {
defer wg.Done()
for value := range ch {
fmt.Println("Consumed:", value)
time.Sleep(150 * time.Millisecond)
}
}
// Select statement for channel operations
func fibonacci(c, quit chan int) {
x, y := 0, 1
for {
select {
case c <- x:
x, y = y, x+y
case <-quit:
fmt.Println("Quit signal received")
return
}
}
}
func main() {
// Basic goroutines
fmt.Println("Starting goroutines...")
go printNumbers(1)
go printNumbers(2)
time.Sleep(1 * time.Second) // Wait for goroutines to finish
// Channel basics
fmt.Println("\nChannel operations:")
ch := make(chan string)
go func() {
ch <- "Hello from goroutine!"
}()
message := <-ch
fmt.Println(message)
// Buffered channel
bufferedCh := make(chan int, 3)
bufferedCh <- 1
bufferedCh <- 2
bufferedCh <- 3
fmt.Printf("Buffered channel length: %d, capacity: %d\n",
len(bufferedCh), cap(bufferedCh))
// Range over channel
fmt.Println("\nCounting with channel:")
for num := range count(5) {
fmt.Println("Received:", num)
}
// Producer-consumer with WaitGroup
fmt.Println("\nProducer-Consumer pattern:")
var wg sync.WaitGroup
prodCh := make(chan int, 5)
wg.Add(2)
go producer(prodCh, &wg)
go consumer(prodCh, &wg)
wg.Wait()
// Select statement
fmt.Println("\nFibonacci with select:")
fibCh := make(chan int)
quit := make(chan int)
go func() {
for i := 0; i < 10; i++ {
fmt.Println(<-fibCh)
}
quit <- 0
}()
fibonacci(fibCh, quit)
}
Explanation
O modelo de concorrência de Go é baseado em goroutines (threads leves) e canais (para comunicação entre goroutines). Isso permite execução paralela eficiente.
Common Use Cases
- Paralelizar computações
- Construir interfaces de usuário responsivas
- Desenvolver servidores de alto desempenho
- Lidar com tarefas em segundo plano
Related Go Syntax
Master Concorrência (Goroutines & Canais) in Go
Understanding Concorrência (Goroutines & Canais) is fundamental to writing clean and efficient Go code. This comprehensive guide provides you with practical examples and detailed explanations to help you master this important concept.
Whether you're a beginner learning the basics or an experienced developer looking to refresh your knowledge, our examples cover real-world scenarios and best practices for using Concorrência (Goroutines & Canais) effectively in your Go projects.
Key Takeaways
- Paralelizar computações
- Construir interfaces de usuário responsivas
- Desenvolver servidores de alto desempenho