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

Pointers को Dereference करना

Dereferencing खज़ाने का नक्शा सिर्फ देखने के बजाय उसके X को follow करके असल में खज़ाना खोदने जैसा है।
Syntax
go
value := *pointer
*pointer = newValue

Reading Through a Pointer

किसी pointer variable से पहले * रखना उस value को पढ़ता है जिसे वह point करता है। यह वह तरीका है जिससे सिर्फ एक pointer रखने वाला code अब भी underlying data तक पहुंच सकता है।

उदाहरण: Reading Through 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() {
	// Short declaration: create age and infer the type
	age := 30
	// Short declaration: create p and infer the type
	p := &age
	// Print the values followed by a newline
	fmt.Println("age via pointer:", *p)
}

Writing Through a Pointer

*p को सिर्फ पढ़ने के बजाय इसे assign करना उस original value को बदल देता है जिसे pointer refer करता है -- यही वह तरीका है जिससे कोई function किसी pointer parameter के ज़रिए caller का variable modify कर सकता है।

उदाहरण: Writing Through 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() {
	// Short declaration: create age and infer the type
	age := 30
	// Short declaration: create p and infer the type
	p := &age
	*p = 31
	// Print the values followed by a newline
	fmt.Println("age is now:", age)
}

Nil Pointer Dereferencing Panics

कोई pointer जिसे address assign नहीं किया गया (इसकी zero value) nil है, और इसे dereference करने की कोशिश program को एक runtime panic से crash कर देती है -- address zero पर पढ़ने या लिखने के लिए कोई value नहीं।

Note: ऐसे किसी pointer को dereference करने से पहले हमेशा 'if p != nil' जांचें जो शायद set न हुआ हो।

उदाहरण: Nil Pointer Dereferencing Panics

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

// Import the fmt package
import "fmt"

// Define the function safeRead
func safeRead(p *int) {
	// Check whether p == nil
	if p == nil {
		// Print the values followed by a newline
		fmt.Println("pointer is nil, nothing to read")
		return
	}
	// Print the values followed by a newline
	fmt.Println("value:", *p)
}

// main is where the program starts running
func main() {
	// Declare p with the zero value of its type
	var p *int
	safeRead(p)
	x := 5
	safeRead(&x)
}

Automatic Dereferencing for Struct Fields

किसी pointer के ज़रिए struct field access करते समय, Go अपने आप आपके लिए इसे dereference करता है -- p.Field, (*p).Field का shorthand है, इसलिए आपको field access के लिए शायद ही कभी explicit dereference लिखना पड़े।

उदाहरण: Automatic Dereferencing for Struct Fields

markup
package main

import "fmt"

type Point struct {
	X, Y int
}

func main() {
	p := &Point{X: 1, Y: 2}
	fmt.Println(p.X, p.Y) // shorthand for (*p).X, (*p).Y
}
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 करना, जो तुरंत एक runtime panic का कारण बनता है ('invalid memory address or nil pointer dereference')।
  2. यह भूल जाना कि * दोनों के लिए चाहिए एक pointer type declare करना (var p *int) और एक pointer value को dereference करना (*p) -- context तय करता है कौन सा।
  3. किसी ऐसी value को dereference करने की कोशिश करना जो असल में pointer नहीं है, जिसे compiler compile time पर reject करता है।
चैप्टर सारांश
  • *p से dereferencing उस value को पढ़ती या लिखती है जिसे कोई pointer point करता है।
  • किसी dereferenced pointer के ज़रिए assign करना, *p = value, original variable को बदल देता है।
  • एक nil pointer को dereference करना runtime पर panic करता है -- dereference करने से पहले हमेशा सुनिश्चित करें कि pointer non-nil है।
  • किसी pointer के ज़रिए struct field access, p.Field, अपने आप dereferenced है -- (*p).Field लिखने की ज़रूरत नहीं।
🔒

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.