← Back to Go Course | Chapter 11: Concurrency | Lesson 4 of 8

select Statement क्या है

select एक साथ कई दरवाज़ों पर इंतज़ार करने और जो भी पहले खुले उसके ज़रिए अंदर जाने जैसा है।
Syntax
go
select {
case v := <-ch1:
    // received from ch1
case ch2 <- value:
    // sent to ch2
default:
    // no channel ready
}

Waiting on Multiple Channels

select तब तक block करता है जब तक इसमें listed कोई एक channel operation आगे बढ़ सके, फिर उस case का body चलाता है -- यही वह तरीका है जिससे कोई goroutine कई channels में से जो भी पहले एक value produce करे उसका जवाब दे सकता है।

उदाहरण: Waiting on Multiple Channels

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Short declaration: create ch1 and infer the type
	ch1 := make(chan string, 1)
	// Short declaration: create ch2 and infer the type
	ch2 := make(chan string, 1)
	ch1 <- "from channel one"

	select {
	case msg1 := <-ch1:
		fmt.Println(msg1)
	case msg2 := <-ch2:
		fmt.Println(msg2)
	}
}

Non-Blocking select with default

एक default case जोड़ना select को non-blocking बना देता है: अगर कोई दूसरा case तुरंत तैयार न हो, default branch इंतज़ार करने के बजाय तुरंत चलता है।

उदाहरण: Non-Blocking select with default

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Short declaration: create ch and infer the type
	ch := make(chan int)
	select {
	case v := <-ch:
		fmt.Println("received:", v)
	// default runs when no case matches
	default:
		fmt.Println("no value ready, moving on")
	}
}

Timeouts with select and time.After

select को time.After के साथ combine करना किसी goroutine को किसी deadline के बाद किसी channel का इंतज़ार छोड़ने देता है, जो blocking operations में timeouts जोड़ने का standard Go pattern है।

उदाहरण: Timeouts with select and time.After

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"time"
)

// main is where the program starts running
func main() {
	// Short declaration: create ch and infer the type
	ch := make(chan string)
	select {
	case msg := <-ch:
		fmt.Println(msg)
	case <-time.After(50 * time.Millisecond):
		fmt.Println("timed out waiting for a value")
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि select किसी switch की तरह order में cases जांचता है -- जब कई cases तैयार हों, Go top-to-bottom के बजाय randomly एक चुनता है।
  2. बिना default और कभी तैयार न होने वाले किसी case के साथ एक select लिखना, goroutine को हमेशा के लिए block करते हुए।
  3. यह भूल जाना कि एक default case select को non-blocking बना देता है, कोई channel तैयार न होने पर तुरंत आगे बढ़ते हुए।
चैप्टर सारांश
  • select किसी goroutine को एक साथ कई channel operations पर इंतज़ार करने देता है।
  • जब कई cases एक साथ तैयार हों, select randomly एक चुनता है -- कोई priority order नहीं है।
  • एक default case select को non-blocking बनाता है, कोई channel तैयार न होने पर तुरंत चलते हुए।
  • select आमतौर पर channel operations पर timeouts implement करने के लिए time.After के साथ उपयोग होता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.