Variadic Functions क्या हैं
In this page:
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
// 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())
}
Login to try C/C++/Java/PHP code in the editor
Spreading a Slice into Variadic Arguments
अगर आपके पास पहले से एक slice है और आप इसके elements को individual variadic arguments के रूप में पास करना चाहते हैं, call site पर slice को एक value के रूप में पास करने के बजाय इसके बाद '...' जोड़ें।
उदाहरण: Spreading a Slice into Variadic Arguments
// 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...))
}
Login to try C/C++/Java/PHP code in the editor
Mixing Regular and Variadic Parameters
एक variadic parameter को इससे पहले regular parameters के साथ combine किया जा सकता है, जब तक यह list में आखिरी parameter रहे -- एक logger जैसे functions के लिए एक आम pattern जो हमेशा एक prefix plus values की एक variable संख्या लेता है।
उदाहरण: Mixing Regular and Variadic Parameters
// 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)
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि variadic parameter को parameter list में आखिरी होना चाहिए -- Go सिर्फ एक की अनुमति देता है, बिल्कुल आखिर में।
- '...' spread operator के बिना किसी slice को सीधे एक variadic parameter को पास करने की कोशिश करना, जो compile होने में fail होता है।
- यह मान लेना कि कोई 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: