नामित Return Values
func name(param type) (result type, err error) {
// body
return
}
Declaring Named Returns
सिर्फ return types list करने के बजाय, कोई function signature में हर return value को एक नाम दे सकता है, जो इसके purpose को document भी करता है और इसे इसकी zero value से initialized एक उपयोग करने योग्य local variable के रूप में पहले से declare भी करता है।
उदाहरण: Declaring Named Returns
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define the function rectangleStats
func rectangleStats(w, h float64) (area, perimeter float64) {
area = w * h
perimeter = 2 * (w + h)
return
}
// main is where the program starts running
func main() {
// Short declaration: create a, p and infer the type
a, p := rectangleStats(4, 3)
// Print the values followed by a newline
fmt.Println("area:", a, "perimeter:", p)
}
Login to try C/C++/Java/PHP code in the editor
Bare return Statements
जब return values named हों, बिना arguments वाला एक साधारण return अपने आप उन named variables में जो भी वर्तमान में हो उसे return कर देता है, आपको variable names दोहराने से बचाते हुए।
उदाहरण: Bare return Statements
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define the function splitName
func splitName(full string) (first, last string) {
first = "Go"
last = "Pher"
return
}
// main is where the program starts running
func main() {
// Short declaration: create f, l and infer the type
f, l := splitName("gopher")
// Print the values followed by a newline
fmt.Println(f, l)
}
Login to try C/C++/Java/PHP code in the editor
Named Returns with error
Named returns value/error pattern के लिए खासतौर पर आम हैं, जहां error variable को err नाम देना function body को स्वाभाविक रूप से पढ़ने योग्य बनाता है जैसे आप इसे return करने से पहले conditionals के अंदर सेट करते हैं।
उदाहरण: Named Returns with error
// 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 validateAge
func validateAge(age int) (valid bool, err error) {
// Check whether age < 0
if age < 0 {
err = errors.New("age cannot be negative")
return
}
valid = true
return
}
// main is where the program starts running
func main() {
// Short declaration: create ok, err and infer the type
ok, err := validateAge(-3)
// Print the values followed by a newline
fmt.Println(ok, err)
}
Login to try C/C++/Java/PHP code in the editor
- लंबे functions में named returns का ज़्यादा उपयोग करना, जिससे यह track करना मुश्किल हो जाता है कि हर named value असल में कहां सेट होती है।
- function body के अंदर := से किसी named return variable को shadow करना, एक अलग local variable बनाते हुए जिसे एक bare return नहीं उठाएगा।
- यह मान लेना कि multiple return values के लिए named returns ज़रूरी हैं -- वे बस एक optional documentation/convenience feature हैं।
- Named return values function signature में ही return parameters को नाम देते हैं।
- बिना arguments वाला एक bare return named returns की current values वापस भेजता है।
- Named returns function के अंदर pre-declared, zero-valued local variables की तरह काम करते हैं।
- वे छोटे functions के लिए और हर return value का मतलब document करने के लिए सबसे उपयोगी हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: