← Back to Go Course | Chapter 7: Interfaces | Lesson 3 of 7

Type के Assertions

एक type assertion आप Go को बता रहे हैं 'मुझ पर भरोसा करो, मुझे पता है इस generic box में असल में क्या है' -- और Go जांचता है कि आप सही हैं या नहीं।
Syntax
go
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 बनाते हुए।

Note: जब type पहले से guaranteed न हो हमेशा two-value form को प्राथमिकता दें।

उदाहरण: The Two-Value Form

markup
// 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)
}

The Single-Value Form

ok flag के बिना i.(Type) सिर्फ value return करता है, लेकिन अगर interface असल में वह type नहीं रखता तो तुरंत panic करता है -- तब उपयोगी जब एक mismatch genuinely एक programmer error होगा।

Note: इस form का उपयोग सिर्फ तभी करें जब आप underlying type के बारे में निश्चित हों, या आपने पहले से type switch से जांच लिया हो।

उदाहरण: The Single-Value Form

markup
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)
}

Asserting to an Interface Type

एक type assertion यह भी जांच सकता है कि कोई value वर्तमान में stored वाले से अलग, संकरे interface को satisfy करती है या नहीं, जो तब आम है जब किसी general interface{} के साथ काम करते हों जो optionally अतिरिक्त behavior support करे।

उदाहरण: Asserting to an Interface Type

markup
// 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())
	}
}
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. ok flag के बिना single-value form (v := i.(Type)) उपयोग करना, जो assertion गलत होने पर runtime पर panic करता है।
  2. यह मान लेना कि एक type assertion किसी value को convert करता है, जबकि यह असल में सिर्फ interface के अंदर पहले से stored concrete type को check/extract करता है।
  3. जब 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:

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.