Structs के Pointers
In this page:
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
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Passing Struct Pointers to Functions
struct को खुद पास करने के बजाय इसका pointer पास करना called function को caller का original data modify करने देता है और हर call पर संभावित रूप से बड़े structs copy करने से बचाता है।
उदाहरण: Passing Struct Pointers to Functions
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Field Access via Pointer
किसी struct pointer के ज़रिए fields access और set करना बिल्कुल एक साधारण struct value जैसा dot notation उपयोग करता है, क्योंकि Go field access के लिए pointer को अपने आप dereference करता है।
उदाहरण: Field Access via Pointer
// 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)
}
Login to try C/C++/Java/PHP code in the editor
- कई functions को एक बड़े struct को value से पास करना, अनावश्यक copying का कारण बनते हुए, जब एक pointer ज़्यादा efficient होता।
- किसी value receiver method का उपयोग करना जब मकसद असल में एक method call के ज़रिए struct की अपनी fields modify करना हो।
- यह भूल जाना कि 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: