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

Structs के Pointers

किसी struct का pointer एक shortcut note है जो किसी भी function को बिल्कुल एक जैसे original form तक पहुंचने देता है, बजाय सबके अपनी अलग photocopy पर लिखने के।
Syntax
go
p := &StructType{Field: value}
p.Field = other  // auto-dereferenced

Creating a Pointer to a Struct

किसी struct literal को & से prefix करना एक single expression में struct value दोनों बनाता है और इसका pointer देता है, जो तब बहुत आम है जब किसी function को एक ऐसा object return करना हो जिसे बाद में mutate किया जाएगा।

उदाहरण: Creating a Pointer to a Struct

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 Point
type Point struct {
	X, Y int
}

// main is where the program starts running
func main() {
	// Short declaration: create p and infer the type
	p := &Point{X: 1, Y: 2}
	// Print the values followed by a newline
	fmt.Println(p)
}

Passing Struct Pointers to Functions

struct को खुद पास करने के बजाय इसका pointer पास करना called function को caller का original data modify करने देता है और हर call पर संभावित रूप से बड़े structs copy करने से बचाता है।

उदाहरण: Passing Struct Pointers to Functions

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 Account
type Account struct {
	Balance float64
}

// Define the function deposit
func deposit(a *Account, amount float64) {
	a.Balance += amount
}

// main is where the program starts running
func main() {
	// Short declaration: create acc and infer the type
	acc := Account{Balance: 100}
	deposit(&acc, 50)
	// Print the values followed by a newline
	fmt.Println(acc.Balance)
}

Field Access via Pointer

किसी struct pointer के ज़रिए fields access और set करना बिल्कुल एक साधारण struct value जैसा dot notation उपयोग करता है, क्योंकि Go field access के लिए pointer को अपने आप dereference करता है।

उदाहरण: Field Access via Pointer

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 Book
type Book struct {
	Title  string
	Copies int
}

// main is where the program starts running
func main() {
	// Short declaration: create b and infer the type
	b := &Book{Title: "Go Basics", Copies: 3}
	b.Copies--
	// Print the values followed by a newline
	fmt.Println(b.Title, "copies left:", b.Copies)
}
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. कई functions को एक बड़े struct को value से पास करना, अनावश्यक copying का कारण बनते हुए, जब एक pointer ज़्यादा efficient होता।
  2. किसी value receiver method का उपयोग करना जब मकसद असल में एक method call के ज़रिए struct की अपनी fields modify करना हो।
  3. यह भूल जाना कि struct pointer literals &Type{...} syntax एक step में बनाने और address पाने के लिए उपयोग करते हैं।
चैप्टर सारांश
  • &Type{...} एक struct बनाता है और तुरंत इसका एक pointer return करता है।
  • किसी function को struct pointer पास करना उस function को original struct की fields modify करने देता है।
  • किसी struct pointer पर field access एक साधारण struct value जैसा ही dot syntax उपयोग करता है।
  • methods पर pointer receivers methods को struct की अपनी state mutate करने देने का standard तरीका हैं।
🔒

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.