← Back to Go Course | Chapter 1: Setup & Basics | Lesson 7 of 7

go fmt और go vet का उपयोग

'go fmt' आपके code की spacing को अपने आप साफ़ करता है, और 'go vet' आपके चलाने से पहले इसे छुपी गलतियों के लिए दोबारा जांचता है।
Syntax
go
go fmt ./...
go vet ./...

Formatting Code with go fmt

'go fmt' आपकी source files को उनकी जगह पर Go की एक सच्ची formatting style से मेल खाने के लिए फिर से लिखता है: consistent indentation, spacing, और brace placement।

क्योंकि सबका code एक जैसा दिखता है, code reviews tabs बनाम spaces पर बहस करने के बजाय logic पर focus कर सकते हैं।

Note: Go tooling configure होने के बाद ज़्यादातर editors save पर अपने आप gofmt चलाते हैं।

उदाहरण: Formatting Code with go fmt

bash
go fmt ./...

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

What gofmt Actually Changes

किसी file पर gofmt चलाना indentation को tabs में adjust करता है, struct fields और comments को align करता है, और operators के चारों ओर spacing को normalize करता है, बिना program क्या करता है यह बदले।

यह बिल्कुल cosmetic है लेकिन community convention से अनिवार्य है -- लगभग सभी published Go code gofmt-clean है।

उदाहरण: What gofmt Actually Changes

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Print the values followed by a newline
	fmt.Println("gofmt normalizes spacing, indentation, and alignment")
}

Catching Bugs with go vet

'go vet' आपके code में ऐसे constructs के लिए जांचता है जो अच्छी तरह compile होते हैं लेकिन संभवतः bugs हैं, जैसे एक fmt.Printf call जिसका format string इसके arguments से मेल नहीं खाता, या invalid syntax वाला एक struct tag।

यह एक lightweight static analysis pass है जो production तक पहुंचने से पहले गलतियां पकड़ता है।

Note: 'go vet' अक्सर 'go test' के हिस्से के रूप में अपने आप चलता है।

उदाहरण: Catching Bugs with go vet

bash
go vet ./...

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

A vet-Catchable Mistake

vet द्वारा flag किया गया एक classic उदाहरण Printf-style function को गलत संख्या या type के arguments पास करना है -- code compile होता है क्योंकि Printf कोई भी arguments accept करता है, लेकिन output runtime पर गलत होगा।

नियमित रूप से vet चलाना इस class की error को तुरंत पकड़ता है।

उदाहरण: A vet-Catchable Mistake

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Short declaration: create name and infer the type
	name := "Gopher"
	// Short declaration: create age and infer the type
	age := 5
	// Print using a format string
	fmt.Printf("%s is %d years old\n", name, age)
}
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. सिर्फ 'go fmt' चलाने के बजाय team style से मेल खाने के लिए manually spacing/tabs align करना, जबकि यह एक non-issue है।
  2. suspicious constructs (जैसे mismatched format verbs वाला एक Printf) के बारे में 'go vet' warnings को नज़रअंदाज़ करना क्योंकि code अब भी compile होता है।
  3. unformatted code को version control में commit करना, जिससे बाद में कोई दूसरा उसी file पर gofmt चलाए तो noisy diffs बनते हैं।
चैप्टर सारांश
  • 'go fmt' source files को Go की standard formatting style में अपने आप फिर से लिखता है।
  • Consistent formatting style के बारे में code-review बहस की पूरी category हटा देता है।
  • 'go vet' संभावित bugs के लिए code analyze करता है जो फिर भी compile होते हैं, जैसे गलत Printf verbs या unreachable code।
  • दोनों tools standard Go toolchain में built-in हैं -- किसी अतिरिक्त installation की ज़रूरत नहीं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

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.