Pointers को Dereference करना
In this page:
value := *pointer
*pointer = newValue
Reading Through a Pointer
किसी pointer variable से पहले * रखना उस value को पढ़ता है जिसे वह point करता है। यह वह तरीका है जिससे सिर्फ एक pointer रखने वाला code अब भी underlying data तक पहुंच सकता है।
उदाहरण: Reading Through a Pointer
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Writing Through a Pointer
*p को सिर्फ पढ़ने के बजाय इसे assign करना उस original value को बदल देता है जिसे pointer refer करता है -- यही वह तरीका है जिससे कोई function किसी pointer parameter के ज़रिए caller का variable modify कर सकता है।
उदाहरण: Writing Through a Pointer
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Nil Pointer Dereferencing Panics
कोई pointer जिसे address assign नहीं किया गया (इसकी zero value) nil है, और इसे dereference करने की कोशिश program को एक runtime panic से crash कर देती है -- address zero पर पढ़ने या लिखने के लिए कोई value नहीं।
उदाहरण: Nil Pointer Dereferencing Panics
// 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)
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
Login to try C/C++/Java/PHP code in the editor
- एक nil pointer को dereference करना, जो तुरंत एक runtime panic का कारण बनता है ('invalid memory address or nil pointer dereference')।
- यह भूल जाना कि * दोनों के लिए चाहिए एक pointer type declare करना (var p *int) और एक pointer value को dereference करना (*p) -- context तय करता है कौन सा।
- किसी ऐसी 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: