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

Channels क्या हैं

एक channel दो goroutines के बीच एक pipe जैसा है -- एक छोर messages डालता है, दूसरा छोर उन्हें उठाता है, उसी order में जिसमें वे भेजे गए थे।
Syntax
go
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

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 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)
}

Channels as Synchronization

चूंकि एक receive तब तक block होता है जब तक एक value भेजी न जाए, channels स्वाभाविक रूप से goroutines को synchronize करते हैं -- receiving side बिल्कुल तब तक इंतज़ार करता है जब तक sending goroutine अपने काम में उस point तक न पहुंच जाए।

उदाहरण: Channels as Synchronization

markup
// 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")
}

Closing a Channel

close(ch) से किसी channel को बंद करना signal देता है कि अब और values नहीं भेजी जाएंगी। Receivers इसे comma-ok form से detect कर सकते हैं, और किसी channel पर range बंद होने पर अपने आप रुक जाता है।

Note: सिर्फ sender को ही किसी channel को बंद करना चाहिए -- किसी ऐसे channel को बंद करना जिसे आप सिर्फ पढ़ रहे हैं (या जो पहले से बंद है) एक panic का कारण बनता है।

उदाहरण: Closing a Channel

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)
	// 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)
	}
}
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. किसी ऐसे channel पर भेजना जिससे कोई कभी receive नहीं करेगा, sending goroutine को हमेशा के लिए block करते हुए (deadlock)।
  2. यह भूल जाना कि unbuffered channel send एक receiver तैयार होने तक block करता है, और उल्टा भी -- यह एक rendezvous है, mailbox नहीं।
  3. किसी बंद 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:

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.