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

Variadic Functions क्या हैं

एक variadic function एक ऐसे bag जैसा है जो किसी भी संख्या में items रख सकता है -- आप इसे zero, एक, या सौ arguments दे सकते हैं और यह बस उन सबको इकट्ठा कर लेता है।
Syntax
go
func name(fixed type, args ...type) returnType {
    // args is []type
}

name(a, b, c)
name(a, slice...)

Declaring a Variadic Function

आखिरी parameter के type को '...' से prefix करना caller को उस type के किसी भी संख्या में arguments पास करने देता है, बिल्कुल कोई नहीं सहित -- function के अंदर, वह parameter बस एक regular slice है।

उदाहरण: Declaring a Variadic Function

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

// Import the fmt package
import "fmt"

// Define the function sum
func sum(nums ...int) int {
	// Short declaration: create total and infer the type
	total := 0
	// Loop over nums; the blank identifier _ ignores the index
	for _, n := range nums {
		total += n
	}
	// Send total back to the caller
	return total
}

// main is where the program starts running
func main() {
	// Print the values followed by a newline
	fmt.Println(sum(1, 2, 3, 4))
	fmt.Println(sum())
}

Spreading a Slice into Variadic Arguments

अगर आपके पास पहले से एक slice है और आप इसके elements को individual variadic arguments के रूप में पास करना चाहते हैं, call site पर slice को एक value के रूप में पास करने के बजाय इसके बाद '...' जोड़ें।

उदाहरण: Spreading a Slice into Variadic Arguments

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

// Import the fmt package
import "fmt"

// Define the function sum
func sum(nums ...int) int {
	// Short declaration: create total and infer the type
	total := 0
	// Loop over nums; the blank identifier _ ignores the index
	for _, n := range nums {
		total += n
	}
	// Send total back to the caller
	return total
}

// main is where the program starts running
func main() {
	// Short declaration: create values and infer the type
	values := []int{10, 20, 30}
	fmt.Println(sum(values...))
}

Mixing Regular and Variadic Parameters

एक variadic parameter को इससे पहले regular parameters के साथ combine किया जा सकता है, जब तक यह list में आखिरी parameter रहे -- एक logger जैसे functions के लिए एक आम pattern जो हमेशा एक prefix plus values की एक variable संख्या लेता है।

Note: Variadic parameters को हमेशा signature में आखिर में आना चाहिए।

उदाहरण: Mixing Regular and Variadic Parameters

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

// Import the fmt package
import "fmt"

// Define the function logAll
func logAll(prefix string, values ...int) {
	// Loop over values; the blank identifier _ ignores the index
	for _, v := range values {
		// Print the values followed by a newline
		fmt.Println(prefix, v)
	}
}

// main is where the program starts running
func main() {
	logAll("value:", 1, 2, 3)
}
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. यह भूल जाना कि variadic parameter को parameter list में आखिरी होना चाहिए -- Go सिर्फ एक की अनुमति देता है, बिल्कुल आखिर में।
  2. '...' spread operator के बिना किसी slice को सीधे एक variadic parameter को पास करने की कोशिश करना, जो compile होने में fail होता है।
  3. यह मान लेना कि कोई variadic function zero arguments accept कर सकता है और एक खाली slice पास करने जैसा ही व्यवहार करेगा, पहले len() जांचे बिना।
चैप्टर सारांश
  • '...Type' के रूप में लिखा गया एक variadic parameter किसी function को उस type के किसी भी संख्या में arguments accept करने देता है।
  • function के अंदर, variadic parameter एक regular slice जैसा व्यवहार करता है।
  • spread operator, slice..., का उपयोग करके एक मौजूदा slice को किसी variadic parameter को पास किया जा सकता है।
  • किसी function signature में सिर्फ आखिरी parameter variadic हो सकता है।
🔒

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.