Channels & select·Q183·Medium
An unbuffered channel ( make(chan T) ) is a rendezvous: a send blocks until a receiver is ready, and a receive blocks until a sender is ready. The handoff is…
Channels & select·Q184·Hard
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 & select·Q185·Medium
Operation nil channel open channel closed channel send ch <- v blocks forever blocks until received/buffer space panic receive <-ch blocks forever blocks until value…
Channels & select·Q186·Medium
package main import "fmt" func main() { ch := make(chan int, 3) ch <- 1 ch <- 2 close(ch) for i := range 4 { v, ok := <-ch fmt.Println(i, v, ok) } } Output: 0 1 true 1 2…
Channels & select·Q187·Medium
v, ok := <-ch sets ok to false only when the channel is closed and drained. You need it when: you are inside a select and must notice closure so you can disable the case…
Channels & select·Q188·Medium
The rule: the sender closes, never the receiver , and only when there are no more sends. Closing is a broadcast message that says "no more values". A receiver closing…
Channels & select·Q189·Hard
First, restructure the code if you can, because multiple closers usually means ownership isn't clear. When you genuinely need a "stop" signal that several parties may…
Channels & select·Q190·Medium
If more than one case is ready, select picks one uniformly at random (pseudo-random). Source order gives no priority. The runtime ( selectgo ) shuffles the poll order…
Channels & select·Q191·Hard
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 & select·Q192·Hard
Send and receive on a nil channel block forever, so a select case on a nil channel is never chosen . Setting a channel variable to nil therefore switches that case off…
Channels & select·Q193·Hard
package main import "fmt" func main() { var nilCh chan int closed := make(chan int) close(closed) counts := map[string]int{} for range 1000 { select { case <-nilCh:…
Channels & select·Q194·Medium
The runtime's checkdead triggers when every goroutine is blocked on something that no other goroutine can unblock (channel ops, select{} , sync primitives), and there…
Channels & select·Q195·Medium
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 & select·Q196·Medium
for v := range ch receives until the channel is closed and drained , then exits. It reads one value per iteration (there is no index variable), and ranging over a nil…
Channels & select·Q197·Medium
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 & select·Q198·Medium
// 1. time.After: simplest, fine for one-off selects select { case v := <-ch: use(v) case <-time.After(2 * time.Second): return errors.New("timeout") } // 2. Timer:…
Channels & select·Q199·Hard
for { select { case msg := <-msgs: handle(msg) case <-time.After(time.Minute): // new timer every iteration log.Println("idle") } } Before Go 1.23: every iteration…
Channels & select·Q200·Medium
A Ticker delivers ticks on a fixed schedule through one reusable channel. If the receiver falls behind, the ticker drops ticks instead of queuing them (the channel holds…
Channels & select·Q201·Medium
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 & select·Q202·Hard
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 & select·Q203·Medium
A buffered channel of capacity N acts as a counting semaphore: send to acquire, receive to release. func processAll(ctx context.Context, items []Item, limit int) error {…
Channels & select·Q204·Hard
type Result struct { Job int Out string Err error } func Pool(ctx context.Context, jobs []int, workers int) []Result { jobCh := make(chan int) resCh := make(chan Result)…
Channels & select·Q205·Hard
// LEAKS on timeout func fetch(url string) (string, error) { ch := make(chan string) // unbuffered go func() { ch <- slowGet(url) // blocks forever if nobody receives…
Channels & select·Q206·Medium
struct{} has size 0, so the channel buffer uses no memory for elements, and the type tells the reader that the value means nothing and the event is the point. A send…
Channels & select·Q207·Medium
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 & select·Q208·Medium
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 & select·Q209·Hard
A pipeline is a chain of stages. Each stage receives from an inbound channel, transforms the values, and sends on an outbound channel it owns and closes. The rules:…
Channels & select·Q210·Hard
ch := make(chan int, 1) close(ch) select { case ch <- 1: fmt.Println("sent") default: fmt.Println("default") } It panics with send on closed channel . It does not fall…
Channels & select·Q211·Hard
Yes. The Go memory model defines these synchronization rules for channels: A send on a channel is synchronized before the corresponding receive completes . Closing a…
Channels & select·Q212·Hard
// First successful result wins; the others are cancelled. func First[T any](ctx context.Context, fns ...func(context.Context) (T, error)) (T, error) { ctx, cancel :=…
Channels & select·Q213·Hard
package main import "fmt" func main() { reqs := make(chan chan int) go func() { n := 0 for reply := range reqs { n++ reply <- n } }() for range 3 { r := make(chan int)…
Channels & select·Q214·Medium
// 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…
Channels & select·Q215·Medium
Yes to all three. Channel types are comparable : two channel values are equal if they came from the same make call (the same hchan pointer) or are both nil. So channels…
Channels & select·Q216·Hard
func main() { ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop() jobs := make(chan Job, 100) var wg sync.WaitGroup for…
Channels & select·Q217·Hard
package main import "fmt" func main() { ch := make(chan int) go func() { for i := range 3 { ch <- i } close(ch) }() for { select { case v, ok := <-ch: if !ok { break }…
Channels & select·Q218·Hard
closechan sets the closed flag under the channel lock, removes every waiting sudog from both recvq and sendq , and makes all of those goroutines runnable: Blocked…
Channels & select·Q219·Hard
A channel's capacity is fixed at make time. The Go team has declined to add unbounded channels (the proposal is golang/go#20352) because a bounded buffer gives you…
Channels & select·Q220·Hard
A send copies the value ( elemsize bytes) into the buffer or straight to the receiver. That means: A large struct is copied on every send and again on every receive. For…