← Back to Go Course | Chapter 4: Functions | Lesson 6 of 7

Closures क्या हैं

एक closure एक ऐसा function है जो अपना छोटा backpack उन variables का पैक करता है जहां यह बनाया गया था, ताकि यह बाद में भी उन्हें उपयोग कर सके भले ही यह कहीं और travel करे।
Syntax
go
func outer() func(param type) returnType {
    captured := value
    return func(param type) returnType {
        // uses captured
    }
}

What Makes a Closure

एक closure बस एक function literal है जो अपने बाहर declared किसी variable को refer करता है -- Go अपने आप उस variable को उस function के लिए जीवित और accessible रखता है जब तक closure खुद मौजूद रहे।

उदाहरण: What Makes a Closure

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 message and infer the type
	message := "hello"
	// Short declaration: create greet and infer the type
	greet := func() {
		// Print the values followed by a newline
		fmt.Println(message)
	}
	greet()
}

Closures Share, Not Copy, Captured Variables

चूंकि एक closure अपनी value के snapshot के बजाय variable को खुद capture करता है, closure के अंदर किए बदलाव इसके बाहर दिखाई देते हैं, और बाहर किए बदलाव अगली बार closure चलने पर दिखाई देते हैं।

उदाहरण: Closures Share, Not Copy, Captured Variables

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 count and infer the type
	count := 0
	// Short declaration: create increment and infer the type
	increment := func() {
		count++
	}
	increment()
	increment()
	// Print the values followed by a newline
	fmt.Println("count:", count)
}

Building a Counter Factory

Closures का एक classic उपयोग एक ऐसा function है जो दूसरा function return करता है, जो calls के बीच अपनी private state जीवित रखता है -- जैसे एक counter generator जो हर call पर एक independent, incrementing counter सौंपता है।

Note: makeCounter की हर call एक बिल्कुल नया, independent count variable बनाती है।

उदाहरण: Building a Counter Factory

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

// Import the fmt package
import "fmt"

// Define the function makeCounter
func makeCounter() func() int {
	// Short declaration: create count and infer the type
	count := 0
	// Send func() int { back to the caller
	return func() int {
		count++
		return count
	}
}

// main is where the program starts running
func main() {
	// Short declaration: create counter and infer the type
	counter := makeCounter()
	// Print the values followed by a newline
	fmt.Println(counter())
	fmt.Println(counter())
	fmt.Println(counter())
}
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. यह मान लेना कि हर closure को किसी captured variable की अपनी independent copy मिलती है, जबकि closures असल में उसी variable का एक reference share करते हैं।
  2. Go-1.22 से पहले किसी loop में closures बनाना और यह देखकर हैरान होना कि वे सभी एक जैसी आखिरी loop variable value capture करते हैं।
  3. किसी भी synchronization के बिना कई goroutines से shared state को mutate करने के लिए एक closure उपयोग करना, एक data race का कारण बनते हुए।
चैप्टर सारांश
  • एक closure एक function literal है जो अपनी body के बाहर से variables को reference करता है।
  • Captured variables reference से share होते हैं, copy नहीं होते -- closure के अंदर बदलाव बाहर दिखाई देते हैं, और उल्टा भी।
  • Closures आमतौर पर function factories बनाने के लिए उपयोग होते हैं, जैसे counters या accumulators।
  • Go 1.22 से, हर loop iteration को एक नया variable मिलता है, loops के अंदर closures को सहज रूप से व्यवहार करने देते हुए।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.