Senior
Do you write a lot of asynchronous code at your current job?
sobes.tech AI
Answer from AI
In Go, asynchrony is implemented through goroutines and channels, which allows for efficient writing of concurrent code. In my current job, I actively use goroutines for parallel execution of tasks, such as processing requests or background operations. This helps improve the performance and responsiveness of applications.
Example of simple asynchronous code in Go:
package main
import (
"fmt"
"time"
)
func worker(id int) {
fmt.Printf("Worker %d starting\n", id)
time.Sleep(time.Second)
fmt.Printf("Worker %d done\n", id)
}
func main() {
for i := 1; i <= 3; i++ {
go worker(i) // start goroutine
}
time.Sleep(2 * time.Second) // wait for goroutines to finish
}
Thus, asynchronous code in Go is a natural and widely used approach.