← Back to Go Course | Chapter 6: Structs & Methods | Lesson 4 of 6

Value और Pointer Receivers में अंतर

एक value receiver आपके struct के एक photocopy पर काम करता है, लेकिन एक pointer receiver वापस पहुंचकर असली original edit करता है।
Syntax
go
func (r Type) valueMethod() {
    // receives a copy
}

func (r *Type) pointerMethod() {
    // can modify r
}

Value Receivers Copy

एक value receiver वाला method struct की अपनी independent copy पाता है -- इसके अंदर किए कोई भी बदलाव method के return होते ही अदृश्य हो जाते हैं, क्योंकि वे copy के साथ हुए, original के साथ नहीं।

उदाहरण: Value Receivers Copy

markup
package main

import "fmt"

type Counter struct {
	Value int
}

func (c Counter) IncrementValue() {
	c.Value++ // modifies the copy only
}

func main() {
	c := Counter{Value: 0}
	c.IncrementValue()
	fmt.Println("still:", c.Value)
}

Pointer Receivers Mutate

एक pointer receiver वाला method इसके address के ज़रिए सीधे original struct पर operate करता है, इसलिए method के अंदर किए बदलाव इसके return होने के बाद caller को दिखाई देते हैं।

उदाहरण: Pointer Receivers Mutate

markup
package main

import "fmt"

type Counter struct {
	Value int
}

func (c *Counter) IncrementValue() {
	c.Value++ // modifies the original
}

func main() {
	c := Counter{Value: 0}
	c.IncrementValue()
	fmt.Println("now:", c.Value)
}

Automatic Addressing

Go आपको किसी साधारण (addressable) struct value पर सीधे एक pointer-receiver method call करने देता है, जैसे c.IncrementValue(), पीछे अपने आप इसका address लेते हुए -- आपको खुद (&c).IncrementValue() लिखने की ज़रूरत नहीं।

उदाहरण: Automatic Addressing

markup
package main

import "fmt"

type Account struct {
	Balance float64
}

func (a *Account) Deposit(amount float64) {
	a.Balance += amount
}

func main() {
	acc := Account{Balance: 100}
	acc.Deposit(50) // Go automatically does (&acc).Deposit(50)
	fmt.Println(acc.Balance)
}
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. struct modify करने वाले method के लिए एक value receiver उपयोग करना, फिर यह confuse होना कि बदलाव method के बाहर क्यों बना नहीं रहता।
  2. किसी type के methods में असंगत रूप से value और pointer receivers मिलाना, जो किसी interface implementation को समझना ज़्यादा मुश्किल बना सकता है।
  3. छोटे structs पर सिर्फ आदत से एक pointer receiver उपयोग करना जहां एक value receiver सरल और उतना ही efficient होता।
चैप्टर सारांश
  • एक value receiver, func (t Type) M(), value की एक copy पर operate करता है।
  • एक pointer receiver, func (t *Type) M(), original value पर operate करता है और इसे modify कर सकता है।
  • Go किसी addressable value पर pointer-receiver method call करते समय आपके लिए अपने आप address लेता है।
  • pointer receivers उपयोग करें जब method को receiver modify करना हो या struct बड़ा हो।
🔒

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.