Go

Channels & select Interview Questions

38 Go interview questions on channels & select, with detailed answers and code.

0/577 known
38 questions
Channels & selectQ184Hard

A channel value is a pointer to a runtime.hchan struct allocated on the heap. Its main fields are: qcount , dataqsiz : number of elements currently queued and buffer…

Channels & selectQ191Hard

Go has no built-in priority. The usual pattern is a non-blocking check of the high-priority channel first, followed by a blocking select over everything: for { // 1.…

Channels & selectQ195Medium

package main import "fmt" func main() { ch := make(chan int) go func() { for v := range ch { fmt.Println(v) } }() ch <- 1 ch <- 2 close(ch) } It prints 1 and maybe 2 .…

Channels & selectQ197Medium

chan<- T is send-only and <-chan T is receive-only. A bidirectional chan T converts implicitly to either one, but you can't convert back. The compiler rejects receiving…

Channels & selectQ201Medium

Use select with a default case. If no other case is ready, default runs right away. // Non-blocking send: drop if the consumer is behind (e.g. metrics, logs) select {…

Channels & selectQ202Hard

package main import "fmt" func val(s string, v int) int { fmt.Println("eval", s); return v } func chn(s string, c chan int) chan int { fmt.Println("chan", s); return c }…

Channels & selectQ207Medium

package main import "fmt" func main() { var n chan int u := make(chan int) b := make(chan int, 5) b <- 1 b <- 2 <-b close(b) fmt.Println(len(n), cap(n), len(u), cap(u),…

Channels & selectQ208Medium

The Go proverb says "Don't communicate by sharing memory; share memory by communicating", but the practical rule is: Channels are for passing ownership of data,…

Channels & selectQ214Medium

// Token bucket: burst of 5, refill 10/s func NewLimiter(ctx context.Context, rate time.Duration, burst int) <-chan struct{} { tokens := make(chan struct{}, burst) for…

Next topicConcurrency Patterns & sync