nil Pointers क्या हैं
var p *type // nil
if p != nil {
// safe to dereference
}
The Zero Value of a Pointer
एक pointer variable जो declared है लेकिन कभी किसी address को assign नहीं हुआ उसकी value अपने आप nil होती है, 'कुछ भी point नहीं करता' represent करने का Go का तरीका।
उदाहरण: The Zero Value of 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() {
// Declare p with the zero value of its type
var p *int
// Print the values followed by a newline
fmt.Println("p is nil:", p == nil)
}
Login to try C/C++/Java/PHP code in the editor
Checking Before Dereferencing
चूंकि किसी nil pointer को dereference करना panic करता है, जो code शायद एक nil pointer receive करे उसे *p उपयोग करने से पहले explicitly एक if statement से इसे जांचना चाहिए।
उदाहरण: Checking Before Dereferencing
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define the function describe
func describe(p *string) string {
// Check whether p == nil
if p == nil {
// Send "no value" back to the caller
return "no value"
}
// Send *p back to the caller
return *p
}
// main is where the program starts running
func main() {
// Declare name with the zero value of its type
var name *string
fmt.Println(describe(name))
s := "Go"
fmt.Println(describe(&s))
}
Login to try C/C++/Java/PHP code in the editor
Nil Pointers in Structs
एक struct field जो खुद एक pointer type है, struct बिना इसे सेट किए बनने पर default रूप से nil होती है, जो optional data represent करने के लिए उपयोगी है -- लेकिन इसे किसी भी दूसरे pointer की तरह dereference करने से पहले जांचना चाहिए।
उदाहरण: Nil Pointers in Structs
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define a struct type named Profile
type Profile struct {
Nickname *string
}
// main is where the program starts running
func main() {
// Short declaration: create p and infer the type
p := Profile{}
// Check whether p.Nickname == nil
if p.Nickname == nil {
// Print the values followed by a newline
fmt.Println("no nickname set")
}
}
Login to try C/C++/Java/PHP code in the editor
- पहले nil के लिए जांचे बिना किसी pointer को dereference करना, production में एक runtime panic का कारण बनते हुए।
- किसी interface value के अंदर stored nil *T को सीधे nil से compare करना और यह देखकर हैरान होना कि interface खुद nil नहीं है।
- यह भूल जाना कि struct pointer fields default रूप से nil हैं, कोई खाली struct नहीं, और उपयोग से पहले जांचे जाने चाहिए।
- किसी भी pointer type की zero value nil है, मतलब यह कुछ भी point नहीं करता।
- किसी nil pointer को dereference करना तुरंत एक runtime panic का कारण बनता है।
- किसी pointer को == से nil के साथ compare करना यह जांचने का standard तरीका है कि यह set है या नहीं।
- एक non-nil interface अब भी एक nil concrete pointer को wrap कर सकता है -- एक classic Go gotcha जिसे जानना worth है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: