go fmt और go vet का उपयोग
In this page:
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 कर सकते हैं।
उदाहरण: Formatting Code with go fmt
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
// 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")
}
Login to try C/C++/Java/PHP code in the editor
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 तक पहुंचने से पहले गलतियां पकड़ता है।
उदाहरण: Catching Bugs with go vet
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
// 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)
}
Login to try C/C++/Java/PHP code in the editor
- सिर्फ 'go fmt' चलाने के बजाय team style से मेल खाने के लिए manually spacing/tabs align करना, जबकि यह एक non-issue है।
- suspicious constructs (जैसे mismatched format verbs वाला एक Printf) के बारे में 'go vet' warnings को नज़रअंदाज़ करना क्योंकि code अब भी compile होता है।
- 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: