Junior — Middle
How does the @Qualifier annotation affect the selection of a bean in dependency injection?
sobes.tech AI
Answer from AI
Goroutines in Go are lightweight threads managed by the Go runtime. Each goroutine has its own stack memory, initially small (about 2 KB) and can dynamically grow as needed.
Memory interaction occurs as follows:
- All goroutines share a common heap where dynamically allocated objects reside.
- Memory in the heap is managed by the garbage collector, which tracks references from all goroutines.
- To pass data between goroutines, channels are used, providing safe exchange without data races.
- Each goroutine's stack is isolated, allowing efficient context switching without significant overhead.
Example of launching two goroutines that exchange data via a channel:
func main() {
ch := make(chan string)
go func() {
ch <- "hello from goroutine"
}()
msg := <-ch
fmt.Println(msg)
}
Thus, goroutines share the heap memory but have separate stacks, ensuring efficient and safe parallel execution.