← Back to Go Course | Chapter 14: Standard Library & HTTP | Lesson 10 of 10

Go की Best Practices

Best practices साफ़, सुरक्षित, और अगले व्यक्ति (भविष्य के आप सहित) के समझने में आसान Go लिखने पर community का जमा किया wisdom है।

Always Handle Errors

Idiomatic Go हर returned error को actively जांचने वाली चीज़ मानता है, एक optional detail नहीं -- या तो इसे तुरंत handle करें, या इसे explicitly wrap करें और किसी caller तक propagate करें जो कर सके।

उदाहरण: Always Handle Errors

markup
// 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)
}

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

markup
// 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))
}

Accept Interfaces, Return Concrete Types

एक आम Go guideline यह है कि functions अपनी ज़रूरतें पूरी करने वाला सबसे छोटा interface accept करें (callers के लिए उन्हें flexible बनाते हुए), जबकि एक concrete type return करें (result को सीधे काम करना आसान बनाते हुए)।

उदाहरण: Accept Interfaces, Return Concrete Types

markup
// 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))
}

Keep Formatting Consistent

Commit करने से पहले gofmt और go vet चलाना formatting inconsistencies और संभावित bugs को अपने आप पकड़ता है, manual review पर भरोसा करने के बजाय पूरे codebase को एक consistent, machine-verified style में रखते हुए।

Note: इसे अपने आप enforce करने के लिए एक pre-commit hook या CI pipeline में 'gofmt -l .' और 'go vet ./...' जोड़ें।

उदाहरण: Keep Formatting Consistent

bash
gofmt -l .
go vet ./...

⚠️ Run this in your own terminal or Node.js environment.

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. errors को handle या explicitly propagate करने के बजाय _ से discard करके नज़रअंदाज़ करना।
  2. छोटे, केंद्रित functions को early returns के साथ प्राथमिकता देने के बजाय बड़े, गहराई से nested functions लिखना।
  3. 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 करें।

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.