Junior — Middle
How do you pass an array to a function without modifying its contents inside the function?
sobes.tech AI
Answer from AI
Using timeouts to terminate goroutines in Go has the following advantages and disadvantages:
Advantages:
- Allows avoiding infinite waiting or hanging of goroutines.
- Simplifies execution time management, especially when working with external resources (e.g., network requests).
- Helps free resources if an operation takes too long.
Disadvantages:
- Timeouts can lead to premature termination of useful work if set too short.
- Additional logic is required for proper goroutine termination and resource cleanup.
- It is not always obvious how to choose the correct timeout value.
Example of using a timeout with a channel and select:
select {
case result := <-ch:
// process result
case <-time.After(2 * time.Second):
// timeout, interrupt waiting
}