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

अनाम Functions

एक anonymous function बिना नाम वाला एक mini recipe है, जो बिल्कुल वहीं लिखा जाता है जहां ज़रूरत हो, एक बार उपयोग होकर फेंक दिया जाता है -- या बाद में उपयोग के लिए किसी और को सौंप दिया जाता है।
Syntax
go
func(param type) returnType {
    // body
}

varName := func(param type) returnType {
    // body
}

Defining and Calling an Anonymous Function

एक function literal एक नाम छोड़ देता है और इसे एक variable को assign किया जा सकता है, जिसे फिर उस variable name के बाद parentheses उपयोग करके बिल्कुल किसी regular function की तरह call किया जाता है।

उदाहरण: Defining and Calling an Anonymous Function

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 square and infer the type
	square := func(n int) int {
		// Send n * n back to the caller
		return n * n
	}
	// Print the values followed by a newline
	fmt.Println(square(7))
}

Immediately Invoked Function Literals

किसी anonymous function को इसकी closing brace के ठीक बाद parentheses में arguments जोड़कर define होते ही call किया जा सकता है, एक-बार के logic के block को scope करने के लिए उपयोगी।

उदाहरण: Immediately Invoked Function Literals

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 result and infer the type
	result := func(a, b int) int {
		// Send a + b back to the caller
		return a + b
	}(3, 4)
	// Print the values followed by a newline
	fmt.Println(result)
}

Anonymous Functions as Arguments

Anonymous functions अक्सर सीधे दूसरे functions को arguments के रूप में पास किए जाते हैं जो एक callback की उम्मीद करते हैं, सिर्फ एक जगह उपयोग होने वाले अलग function को declare और नाम देने से बचते हुए।

Note: यह pattern sort.Slice के साथ आम है, जो एक anonymous comparison function लेता है।

उदाहरण: Anonymous Functions as Arguments

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

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

// main is where the program starts running
func main() {
	// Short declaration: create nums and infer the type
	nums := []int{5, 2, 8, 1}
	sort.Slice(nums, func(i, j int) bool {
		// Send nums[i] < nums[j] back to the caller
		return nums[i] < nums[j]
	})
	// Print the values followed by a newline
	fmt.Println(nums)
}
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. किसी variable को assign किए anonymous function को func keyword के अंदर एक अनावश्यक नाम देना, जिसकी Go अनुमति नहीं देता (func literals का कोई नाम नहीं होता)।
  2. किसी anonymous function literal को असल में तुरंत invoke करने (एक IIFE) के लिए इसके बाद '()' जोड़ना भूल जाना, इसे सिर्फ defined छोड़ते हुए लेकिन कभी call न करते हुए।
  3. गहराई से nested anonymous functions का ज़्यादा उपयोग करना जहां एक छोटा named function कहीं ज़्यादा readable होता।
चैप्टर सारांश
  • एक anonymous function बिना नाम वाला एक function literal है, जो इसके उपयोग होने की जगह inline defined है।
  • Anonymous functions को एक variable को assign किया जा सकता है और इसके ज़रिए call किया जा सकता है।
  • उन्हें definition के बाद literal में () जोड़कर तुरंत invoke भी किया जा सकता है।
  • वे आमतौर पर छोटे callback arguments या goroutines के लिए उपयोग होते हैं।
🔒

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.