1. Which keyword is used to create a new goroutine in Go?
Answer:
Explanation:
The 'go' keyword is used in Go to launch a new goroutine, which is a lightweight thread managed by the Go runtime.
2. What is a goroutine in Go?
Answer:
Explanation:
A goroutine is a lightweight thread of execution managed by the Go runtime, used for handling concurrent tasks.
3. How do you communicate between goroutines in Go?
Answer:
Explanation:
Channels are the preferred way to communicate between goroutines in Go. They provide a way to transfer data between concurrently running goroutines.
4. What is a channel in Go?
Answer:
Explanation:
A channel in Go is a conduit through which goroutines can communicate with each other, used to send and receive values.
5. How do you create a new channel in Go?
Answer:
Explanation:
Channels in Go are created using the make function, like 'make(chan Type)'.
6. What is a buffered channel in Go?
Answer:
Explanation:
A buffered channel in Go has a capacity to store a limited number of values without a corresponding receiver for those values.
7. How do you specify a buffered channel in Go?
Answer:
Explanation:
Buffered channels in Go are created using the make function with a specified capacity, like 'make(chan Type, size)'.
8. What happens when a goroutine tries to write to a full buffered channel in Go?
Answer:
Explanation:
If a goroutine tries to write to a full buffered channel, it will block until another goroutine reads from the channel and frees up space.
9. What is the 'select' statement used for in Go?
Answer:
Explanation:
The 'select' statement in Go allows a goroutine to wait on multiple communication operations, proceeding with one that is ready to communicate.
10. What does closing a channel signify in Go?
Answer:
Explanation:
Closing a channel in Go indicates that no more values will be sent on it. Receivers can still receive data already in the channel.
11. Can a closed channel in Go be reopened?
Answer:
Explanation:
Once a channel is closed in Go, it cannot be reopened. Attempting to send data on a closed channel will cause a panic.
12. How do you read from a channel until it is closed in Go?
Answer:
Explanation:
A for loop combined with the range keyword is used to read from a channel until it is closed in Go.
13. What is a deadlock in the context of Go concurrency?
Answer:
Explanation:
A deadlock occurs in Go when goroutines wait on each other to complete, but none of them can proceed, resulting in a standstill.
14. Is it possible to have multiple goroutines receive from the same channel in Go?
Answer:
Explanation:
In Go, multiple goroutines can receive from the same channel, allowing for concurrent processing of channel data.
15. What does the 'sync' package in Go provide for concurrency control?
Answer:
Explanation:
The 'sync' package in Go provides concurrency primitives such as Mutexes for mutual exclusion and WaitGroups for waiting for a collection of goroutines to finish.