Type के Assertions
value := iface.(Type)
value, ok := iface.(Type)
The Two-Value Form
v, ok := i.(Type) दिए गए type के रूप में concrete value निकालने की कोशिश करता है, सफलता पर ok को true सेट करते हुए या असफलता पर false (v zero value के साथ) -- यह कभी panic नहीं करता, इसे सुरक्षित default बनाते हुए।
उदाहरण: The Two-Value Form
// 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() {
var i interface{} = "hello"
// Short declaration: create s, ok and infer the type
s, ok := i.(string)
// Print the values followed by a newline
fmt.Println(s, ok)
// Short declaration: create n, ok and infer the type
n, ok := i.(int)
// Print the values followed by a newline
fmt.Println(n, ok)
}
Login to try C/C++/Java/PHP code in the editor
The Single-Value Form
ok flag के बिना i.(Type) सिर्फ value return करता है, लेकिन अगर interface असल में वह type नहीं रखता तो तुरंत panic करता है -- तब उपयोगी जब एक mismatch genuinely एक programmer error होगा।
उदाहरण: The Single-Value Form
package main
import "fmt"
func main() {
var i interface{} = 42
n := i.(int) // safe here, we know it's an int
fmt.Println(n + 8)
}
Login to try C/C++/Java/PHP code in the editor
Asserting to an Interface Type
एक type assertion यह भी जांच सकता है कि कोई value वर्तमान में stored वाले से अलग, संकरे interface को satisfy करती है या नहीं, जो तब आम है जब किसी general interface{} के साथ काम करते हों जो optionally अतिरिक्त behavior support करे।
उदाहरण: Asserting to an Interface Type
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define an interface named Speaker (a set of method signatures)
type Speaker interface {
Speak() string
}
// Define a struct type named Dog
type Dog struct{}
func (Dog) Speak() string { return "Woof" }
// main is where the program starts running
func main() {
var i interface{} = Dog{}
// Check whether s, ok := i.(Speaker); ok
if s, ok := i.(Speaker); ok {
// Print the values followed by a newline
fmt.Println(s.Speak())
}
}
Login to try C/C++/Java/PHP code in the editor
- ok flag के बिना single-value form (v := i.(Type)) उपयोग करना, जो assertion गलत होने पर runtime पर panic करता है।
- यह मान लेना कि एक type assertion किसी value को convert करता है, जबकि यह असल में सिर्फ interface के अंदर पहले से stored concrete type को check/extract करता है।
- जब pointer बनाम value receiver का अंतर मायने रखे (जैसे interface में stored *T बनाम T) तब गलत concrete type के लिए assert करना।
- एक type assertion, i.(Type), किसी interface value में stored concrete value निकालता है।
- single-value form assertion fail होने पर panic करता है -- इसका उपयोग सिर्फ तभी करें जब आप type के बारे में निश्चित हों।
- two-value form, v, ok := i.(Type), mismatch पर panic करने के बजाय false return करता है।
- Type assertions किसी interface{} या दूसरे interface में store करने के बाद एक concrete type वापस पाने का तरीका हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: