What is the output of this Go program using an unbuffered channel?
package main import ( "fmt" ) func main() { ch := make(chan int) go func() { ch <- 42 }() fmt.Println(<-ch) }
Think about how unbuffered channels block until both sender and receiver are ready.
The unbuffered channel blocks the goroutine sending 42 until the main goroutine receives it. So the program prints 42.
What will this Go program print?
package main import ( "fmt" ) func main() { ch := make(chan int, 2) ch <- 10 ch <- 20 fmt.Println(<-ch) fmt.Println(<-ch) }
Buffered channels allow sending without immediate receiving until full.
The buffered channel holds two values. The first receive gets 10, the second gets 20.
What happens when this Go program runs?
package main func main() { ch := make(chan int) ch <- 1 <-ch }
Consider that sending on an unbuffered channel blocks until a receiver is ready.
The send blocks forever because no goroutine is receiving yet, causing a deadlock panic.
What will be the output of this Go program?
package main import ( "fmt" ) func main() { ch := make(chan int, 3) ch <- 5 ch <- 10 fmt.Println(len(ch), cap(ch)) }
len returns the number of elements in the channel buffer, cap returns its capacity.
Two values are sent, so len is 2. Capacity was set to 3.
Which statement correctly describes the difference between buffered and unbuffered channels in Go?
Think about how sending and receiving synchronize in each channel type.
Unbuffered channels require sender and receiver to be ready simultaneously, causing blocking. Buffered channels allow sending up to their capacity without blocking.