Junior — Middle
Is it possible to implement full parallel task processing in Go?
sobes.tech AI
Answer from AI
Yes, the Go language can implement full parallel task processing. Go provides built-in support for concurrency through goroutines — lightweight threads managed by the Go runtime.
Goroutines allow functions to run in parallel, and channels facilitate safe data exchange between them.
Example of parallel processing:
package main
import (
"fmt"
"time"
)
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
fmt.Printf("Worker %d processing job %d\n", id, j)
time.Sleep(time.Second) // simulate work
results <- j * 2
}
}
func main() {
jobs := make(chan int, 5)
results := make(chan int, 5)
for w := 1; w <= 3; w++ {
go worker(w, jobs, results)
}
for j := 1; j <= 5; j++ {
jobs <- j
}
close(jobs)
for a := 1; a <= 5; a++ {
fmt.Println("Result:", <-results)
}
}
Thus, Go allows efficient implementation of parallel processing with minimal effort.