शून्य Values
In this page:
var name type // zero value: 0, 0.0, false, "", nil
Zero Values for Basic Types
जब आप var से बिना किसी initializer के एक variable declare करते हैं, Go अपने आप इसे उस type की zero value पर सेट करता है: numeric types को 0 मिलता है, bool को false, और string को "" (खाली string)। यह गारंटी देता है कि हर variable एक predictable, सुरक्षित state में शुरू हो।
उदाहरण: Zero Values for Basic Types
// 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() {
// Declare count with the zero value of its type
var count int
// Declare price with the zero value of its type
var price float64
var active bool
var label string
// Print the values followed by a newline
fmt.Println(count, price, active, label)
}
Login to try C/C++/Java/PHP code in the editor
Zero Values for Structs
किसी struct की zero value एक ऐसा struct है जहां हर field अपने type की zero value पर सेट है -- इसलिए एक zero-value Person struct का Name खाली और Age 0 होता है, बिना panic के उपयोग के लिए तैयार।
उदाहरण: Zero Values for Structs
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define a struct type named Person
type Person struct {
Name string
Age int
}
// main is where the program starts running
func main() {
// Declare p with the zero value of its type
var p Person
// Print the values followed by a newline
fmt.Println("name:", p.Name, "age:", p.Age)
}
Login to try C/C++/Java/PHP code in the editor
nil as a Zero Value
Reference-जैसे types -- pointers, slices, maps, channels, functions, और interfaces -- के पास अपनी zero value के रूप में nil होता है, 'अभी तक कुछ भी point नहीं कर रहा' represent करते हुए।
कौन सी types nil में और कौन सी एक concrete खाली value में zero होती हैं यह समझना beginner bugs की एक पूरी class से बचाता है।
उदाहरण: nil as a Zero Value
// 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() {
// Declare s with the zero value of its type
var s []int
// Declare m with the zero value of its type
var m map[string]int
// Print the values followed by a newline
fmt.Println("nil slice:", s, "is nil:", s == nil)
// Print the values followed by a newline
fmt.Println("nil map:", m, "is nil:", m == nil)
}
Login to try C/C++/Java/PHP code in the editor
Why Zero Values Matter
चूंकि हर variable declare होते ही अपने आप उपयोग करने योग्य होता है, Go code उस बहुत सारी defensive null-checking को skip कर सकता है जो दूसरी languages मांगती हैं -- एक int हमेशा एक असली number है, कभी कोई खास undefined marker नहीं।
उदाहरण: Why Zero Values Matter
package main
import "fmt"
func main() {
var total int
amounts := []int{10, 20, 30}
for _, a := range amounts {
total += a // total starts safely at 0
}
fmt.Println("total:", total)
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि सभी types के लिए एक uninitialized variable nil/undefined है, जबकि numeric और boolean types को असल में 0 और false मिलते हैं, nil नहीं।
- यह भूल जाना कि एक zero-value struct के पास अभी भी उपयोग करने योग्य (zeroed) fields हैं, access करना कोई error नहीं है।
- किसी zero-value slice और map को एक जैसा treat करना -- एक nil slice में append करना सुरक्षित है, लेकिन एक nil map write पर panic करता है।
- Go में हर type की एक अच्छी तरह परिभाषित zero value होती है जो तब उपयोग होती है जब कोई variable बिना initializer के declare हो।
- Numeric types default रूप से 0, bool false, और string एक खाली string होते हैं।
- Pointers, slices, maps, channels, funcs, और interfaces default रूप से nil होते हैं।
- Zero values का मतलब है Go variables हमेशा एक valid, predictable state में हैं -- कभी undefined नहीं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: