कई Return Values
func name(param type) (type1, type2) {
// body
return value1, value2
}
res1, res2 := name(arg)
Returning Two Values
किसी function का signature parentheses में कई return types list कर सकता है, और return statement हर एक के लिए comma-separated एक matching value देता है।
उदाहरण: Returning Two Values
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define the function divide
func divide(a, b int) (int, int) {
// Send a / b, a % b back to the caller
return a / b, a % b
}
// main is where the program starts running
func main() {
// Short declaration: create quotient, remainder and infer the type
quotient, remainder := divide(17, 5)
// Print the values followed by a newline
fmt.Println("quotient:", quotient, "remainder:", remainder)
}
Login to try C/C++/Java/PHP code in the editor
The Value, error Pattern
Go में multiple return values का सबसे idiomatic उपयोग एक result को एक error के साथ जोड़ना है: function सफलता पर error के लिए nil, या असफलता पर एक non-nil error (और अक्सर एक zero-value result) return करता है।
उदाहरण: The Value, error Pattern
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"errors"
"fmt"
)
// Define the function safeDivide
func safeDivide(a, b int) (int, error) {
// Check whether b == 0
if b == 0 {
return 0, errors.New("division by zero")
}
// Send a / b, nil back to the caller
return a / b, nil
}
// main is where the program starts running
func main() {
// Short declaration: create result, err and infer the type
result, err := safeDivide(10, 0)
// Handle the error if one occurred
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println("result:", result)
}
Login to try C/C++/Java/PHP code in the editor
Discarding Unwanted Return Values
जब कोई function कई values return करे लेकिन आपको उनमें से सिर्फ कुछ चाहिए, blank identifier _ आपको बाकी को unused variables declare किए बिना discard करने देता है।
उदाहरण: Discarding Unwanted Return Values
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define the function minMax
func minMax(nums []int) (int, int) {
// Short declaration: create min, max and infer the type
min, max := nums[0], nums[0]
// Loop over nums; the blank identifier _ ignores the index
for _, n := range nums {
// Check whether n < min
if n < min {
min = n
}
// Check whether n > max
if n > max {
max = n
}
}
// Send min, max back to the caller
return min, max
}
func main() {
_, max := minMax([]int{4, 9, 1, 7})
fmt.Println("max:", max)
}
Login to try C/C++/Java/PHP code in the editor
- किसी function call से error return value को नज़रअंदाज़ करना, यह मानते हुए कि पहली value हमेशा उपयोग के लिए सुरक्षित है।
- जिन्हें skip किया जा रहा है उनके लिए _ उपयोग किए बिना सिर्फ कुछ returned values को assign करने की कोशिश करना।
- यह भूल जाना कि सभी returned values को capture (या _ से discard) किया जाना चाहिए -- आप बस positionally पहली नहीं ले सकते।
- Go functions कई values return कर सकते हैं, return के बाद commas से अलग।
- सबसे आम pattern एक result और एक error साथ return करना है।
- हर returned value को एक variable को assign करना चाहिए या explicitly _ से discard करना चाहिए।
- Multiple return values दूसरी languages में देखे गए output parameters या wrapper objects की ज़रूरत हटा देते हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: