Junior — Middle
Explain what non-blocking calls are in the context of working with goroutines and how they are implemented.
sobes.tech AI
Answer from AI
Non-blocking calls in the context of goroutines are operations that do not cause the current goroutine to wait for the completion of an operation, allowing it to continue execution. In Go, this is often associated with the use of channels and the select statement with default, to attempt to send or receive data from a channel without blocking if the operation is not possible.
Example of a non-blocking send to a channel:
ch := make(chan int)
select {
case ch <- 1: // if the channel is ready to accept a value
fmt.Println("Sent")
default: // if cannot send — do not block
fmt.Println("Channel busy, not blocking")
}
Thus, the goroutine does not block if the operation with the channel is impossible, and continues working. This allows efficient management of concurrency without waiting.