Channels क्या हैं
ch := make(chan type)
ch <- value // send
value := <-ch // receive
close(ch)
Creating and Using a Channel
make(chan Type) एक channel बनाता है जो goroutines के बीच उस type की values ले जाता है। Values ch <- value से भेजी जाती हैं और <-ch से प्राप्त होती हैं, और default रूप से ये operations तब तक block होते हैं जब तक दूसरा पक्ष तैयार न हो।
उदाहरण: Creating and Using a Channel
// 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 string)
// Start this call in a new goroutine (runs concurrently)
go func() {
ch <- "message from goroutine"
}()
// Short declaration: create msg and infer the type
msg := <-ch
// Print the values followed by a newline
fmt.Println(msg)
}
Login to try C/C++/Java/PHP code in the editor
Channels as Synchronization
चूंकि एक receive तब तक block होता है जब तक एक value भेजी न जाए, channels स्वाभाविक रूप से goroutines को synchronize करते हैं -- receiving side बिल्कुल तब तक इंतज़ार करता है जब तक sending goroutine अपने काम में उस point तक न पहुंच जाए।
उदाहरण: Channels as Synchronization
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define the function worker
func worker(done chan bool) {
// Print the values followed by a newline
fmt.Println("working...")
done <- true
}
// main is where the program starts running
func main() {
// Short declaration: create done and infer the type
done := make(chan bool)
// Start this call in a new goroutine (runs concurrently)
go worker(done)
<-done
// Print the values followed by a newline
fmt.Println("worker finished, main continues")
}
Login to try C/C++/Java/PHP code in the editor
Closing a Channel
close(ch) से किसी channel को बंद करना signal देता है कि अब और values नहीं भेजी जाएंगी। Receivers इसे comma-ok form से detect कर सकते हैं, और किसी channel पर range बंद होने पर अपने आप रुक जाता है।
उदाहरण: Closing a Channel
// 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)
// Start this call in a new goroutine (runs concurrently)
go func() {
// Loop: repeat while the condition holds
for i := 1; i <= 3; i++ {
ch <- i
}
close(ch)
}()
for v := range ch {
// Print the values followed by a newline
fmt.Println(v)
}
}
Login to try C/C++/Java/PHP code in the editor
- किसी ऐसे channel पर भेजना जिससे कोई कभी receive नहीं करेगा, sending goroutine को हमेशा के लिए block करते हुए (deadlock)।
- यह भूल जाना कि unbuffered channel send एक receiver तैयार होने तक block करता है, और उल्टा भी -- यह एक rendezvous है, mailbox नहीं।
- किसी बंद channel से पढ़ना और इसके block या error होने की उम्मीद करना, जबकि यह इसके बजाय तुरंत type की zero value return करता है।
- make(chan Type) से बना एक channel goroutines को सुरक्षित रूप से values भेजने और प्राप्त करने देता है।
- किसी unbuffered channel पर भेजना (ch <- v) और प्राप्त करना (<-ch) दोनों sides तैयार होने तक block करते हैं।
- Channels Go की 'share memory by communicating' philosophy के अनुसार goroutines के बीच synchronize और communicate करने का idiomatic तरीका हैं।
- किसी बंद channel से पढ़ना block होने के बजाय तुरंत zero value return करता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: