← Back to Go Course | Chapter 2: Variables & Types | Lesson 5 of 7

शून्य Values

एक zero value वह automatic starter value है जो Go हर नए box को किसी और चीज़ से भरने से पहले देता है, ताकि कुछ भी कभी पूरी तरह खाली न रहे।
Syntax
go
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

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() {
	// 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)
}

Zero Values for Structs

किसी struct की zero value एक ऐसा struct है जहां हर field अपने type की zero value पर सेट है -- इसलिए एक zero-value Person struct का Name खाली और Age 0 होता है, बिना panic के उपयोग के लिए तैयार।

उदाहरण: Zero Values for Structs

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

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 से बचाता है।

Note: किसी nil map या nil slice से पढ़ना सुरक्षित है; सिर्फ किसी nil map में लिखना panic करता है।

उदाहरण: nil as a Zero Value

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() {
	// 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)
}

Why Zero Values Matter

चूंकि हर variable declare होते ही अपने आप उपयोग करने योग्य होता है, Go code उस बहुत सारी defensive null-checking को skip कर सकता है जो दूसरी languages मांगती हैं -- एक int हमेशा एक असली number है, कभी कोई खास undefined marker नहीं।

उदाहरण: Why Zero Values Matter

markup
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)
}
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. यह मान लेना कि सभी types के लिए एक uninitialized variable nil/undefined है, जबकि numeric और boolean types को असल में 0 और false मिलते हैं, nil नहीं।
  2. यह भूल जाना कि एक zero-value struct के पास अभी भी उपयोग करने योग्य (zeroed) fields हैं, access करना कोई error नहीं है।
  3. किसी 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:

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.