select Statement क्या है
In this page:
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
// 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)
}
}
Login to try C/C++/Java/PHP code in the editor
Non-Blocking select with default
एक default case जोड़ना select को non-blocking बना देता है: अगर कोई दूसरा case तुरंत तैयार न हो, default branch इंतज़ार करने के बजाय तुरंत चलता है।
उदाहरण: Non-Blocking select with default
// 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")
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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")
}
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि select किसी switch की तरह order में cases जांचता है -- जब कई cases तैयार हों, Go top-to-bottom के बजाय randomly एक चुनता है।
- बिना default और कभी तैयार न होने वाले किसी case के साथ एक select लिखना, goroutine को हमेशा के लिए block करते हुए।
- यह भूल जाना कि एक 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: