← Back to Go Course | Chapter 8: Pointers | Lesson 4 of 6

nil Pointers क्या हैं

एक nil pointer एक note जैसा है जो कहता है 'यह अभी तक कुछ भी point नहीं करता' -- 'not set' represent करने के लिए उपयोगी, लेकिन अगर आप फिर भी इसे follow करने की कोशिश करें तो खतरनाक।
Syntax
go
var p *type  // nil
if p != nil {
    // safe to dereference
}

The Zero Value of a Pointer

एक pointer variable जो declared है लेकिन कभी किसी address को assign नहीं हुआ उसकी value अपने आप nil होती है, 'कुछ भी point नहीं करता' represent करने का Go का तरीका।

उदाहरण: The Zero Value of a Pointer

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 p with the zero value of its type
	var p *int
	// Print the values followed by a newline
	fmt.Println("p is nil:", p == nil)
}

Checking Before Dereferencing

चूंकि किसी nil pointer को dereference करना panic करता है, जो code शायद एक nil pointer receive करे उसे *p उपयोग करने से पहले explicitly एक if statement से इसे जांचना चाहिए।

Note: किसी lookup, optional field, या fail हो सकने वाले function से आए किसी भी pointer को nil-check करने की आदत डालें।

उदाहरण: Checking Before Dereferencing

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

// Import the fmt package
import "fmt"

// Define the function describe
func describe(p *string) string {
	// Check whether p == nil
	if p == nil {
		// Send "no value" back to the caller
		return "no value"
	}
	// Send *p back to the caller
	return *p
}

// main is where the program starts running
func main() {
	// Declare name with the zero value of its type
	var name *string
	fmt.Println(describe(name))

	s := "Go"
	fmt.Println(describe(&s))
}

Nil Pointers in Structs

एक struct field जो खुद एक pointer type है, struct बिना इसे सेट किए बनने पर default रूप से nil होती है, जो optional data represent करने के लिए उपयोगी है -- लेकिन इसे किसी भी दूसरे pointer की तरह dereference करने से पहले जांचना चाहिए।

उदाहरण: Nil Pointers in 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 Profile
type Profile struct {
	Nickname *string
}

// main is where the program starts running
func main() {
	// Short declaration: create p and infer the type
	p := Profile{}
	// Check whether p.Nickname == nil
	if p.Nickname == nil {
		// Print the values followed by a newline
		fmt.Println("no nickname set")
	}
}
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. पहले nil के लिए जांचे बिना किसी pointer को dereference करना, production में एक runtime panic का कारण बनते हुए।
  2. किसी interface value के अंदर stored nil *T को सीधे nil से compare करना और यह देखकर हैरान होना कि interface खुद nil नहीं है।
  3. यह भूल जाना कि struct pointer fields default रूप से nil हैं, कोई खाली struct नहीं, और उपयोग से पहले जांचे जाने चाहिए।
चैप्टर सारांश
  • किसी भी pointer type की zero value nil है, मतलब यह कुछ भी point नहीं करता।
  • किसी nil pointer को dereference करना तुरंत एक runtime panic का कारण बनता है।
  • किसी pointer को == से nil के साथ compare करना यह जांचने का standard तरीका है कि यह set है या नहीं।
  • एक non-nil interface अब भी एक nil concrete pointer को wrap कर सकता है -- एक classic Go gotcha जिसे जानना worth है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 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.