Go की Best Practices
In this page:
Always Handle Errors
Idiomatic Go हर returned error को actively जांचने वाली चीज़ मानता है, एक optional detail नहीं -- या तो इसे तुरंत handle करें, या इसे explicitly wrap करें और किसी caller तक propagate करें जो कर सके।
उदाहरण: Always Handle Errors
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"fmt"
"strconv"
)
// Define the function parseAge
func parseAge(input string) (int, error) {
// Short declaration: create age, err and infer the type
age, err := strconv.Atoi(input)
// Handle the error if one occurred
if err != nil {
return 0, fmt.Errorf("invalid age %q: %w", input, err)
}
// Send age, nil back to the caller
return age, nil
}
// main is where the program starts running
func main() {
// Short declaration: create age, err and infer the type
age, err := parseAge("thirty")
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println("age:", age)
}
Login to try C/C++/Java/PHP code in the editor
Favor Early Returns Over Deep Nesting
किसी function के ऊपर एक early return के साथ error और edge cases handle करना main logic को एक single indentation level पर रखता है, जो आमतौर पर गहराई से nested if/else chains से पढ़ना आसान है।
उदाहरण: Favor Early Returns Over Deep Nesting
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define the function classify
func classify(age int) string {
// Check whether age < 0
if age < 0 {
// Send "invalid" back to the caller
return "invalid"
}
// Check whether age < 13
if age < 13 {
// Send "child" back to the caller
return "child"
}
if age < 20 {
return "teen"
}
return "adult"
}
// main is where the program starts running
func main() {
fmt.Println(classify(15))
fmt.Println(classify(-1))
}
Login to try C/C++/Java/PHP code in the editor
Accept Interfaces, Return Concrete Types
एक आम Go guideline यह है कि functions अपनी ज़रूरतें पूरी करने वाला सबसे छोटा interface accept करें (callers के लिए उन्हें flexible बनाते हुए), जबकि एक concrete type return करें (result को सीधे काम करना आसान बनाते हुए)।
उदाहरण: Accept Interfaces, Return Concrete Types
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"fmt"
"strings"
)
// Define the function wordCount
func wordCount(r *strings.Reader) int {
// Short declaration: create buf and infer the type
buf := make([]byte, r.Len())
r.Read(buf)
return len(strings.Fields(string(buf)))
}
// main is where the program starts running
func main() {
// Short declaration: create r and infer the type
r := strings.NewReader("Go favors small interfaces")
// Print the values followed by a newline
fmt.Println(wordCount(r))
}
Login to try C/C++/Java/PHP code in the editor
Keep Formatting Consistent
Commit करने से पहले gofmt और go vet चलाना formatting inconsistencies और संभावित bugs को अपने आप पकड़ता है, manual review पर भरोसा करने के बजाय पूरे codebase को एक consistent, machine-verified style में रखते हुए।
उदाहरण: Keep Formatting Consistent
gofmt -l .
go vet ./...
⚠️ Run this in your own terminal or Node.js environment.
- errors को handle या explicitly propagate करने के बजाय _ से discard करके नज़रअंदाज़ करना।
- छोटे, केंद्रित functions को early returns के साथ प्राथमिकता देने के बजाय बड़े, गहराई से nested functions लिखना।
- commit करने से पहले gofmt/'go vet' skip करना, बचने योग्य style और correctness issues को codebase में आने देते हुए।
- हमेशा errors जांचें और handle करें -- इन्हें कभी blank identifier से चुपचाप discard न करें।
- Functions को छोटा और केंद्रित रखें; गहरी nesting से बचने के लिए early returns उपयोग करें।
- style और correctness issues अपने आप पकड़ने के लिए हर commit से पहले gofmt और go vet चलाएं।
- बड़े interfaces के बजाय छोटे, composable interfaces को प्राथमिकता दें, और interfaces accept करते हुए concrete types return करें।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: