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

Type के Switches

एक type switch आपको किसी mystery box से 'तुम असल में किस तरह की चीज़ हो?' पूछने और जवाब के आधार पर अलग code चलाने देता है।
Syntax
go
switch v := iface.(type) {
case Type1:
    // v is Type1
case Type2:
    // v is Type2
default:
    // v has interface type
}

Basic Type Switch

एक type switch किसी switch statement के अंदर interface value के असली concrete type के आधार पर branch करने के लिए special syntax i.(type) उपयोग करता है, matching case का code चलाते हुए।

उदाहरण: Basic Type Switch

markup
// 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(i interface{}) {
	// Choose a branch based on the value of v := i.(type
	switch v := i.(type) {
	case int:
		fmt.Println("int:", v*2)
	case string:
		fmt.Println("string:", v+"!")
	// default runs when no case matches
	default:
		fmt.Println("unknown type")
	}
}

// main is where the program starts running
func main() {
	describe(21)
	describe("hi")
	describe(3.14)
}

Grouping Multiple Types in One Case

किसी type switch में एक single case commas से अलग कई types list कर सकता है, तब उपयोगी जब कई types को एक जैसे handle किया जाना चाहिए -- हालांकि उस combined case के अंदर, v एक specific type के बजाय original interface type रखता है।

उदाहरण: Grouping Multiple Types in One Case

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// Define the function classify
func classify(i interface{}) string {
	// Choose a branch based on the value of i.(type
	switch i.(type) {
	case int, int64, float64:
		return "numeric"
	case string:
		return "text"
	// default runs when no case matches
	default:
		return "other"
	}
}

// main is where the program starts running
func main() {
	// Print the values followed by a newline
	fmt.Println(classify(5))
	// Print the values followed by a newline
	fmt.Println(classify("hi"))
	fmt.Println(classify(true))
}

Handling the nil Case

एक type switch explicitly किसी interface value को handle करने के लिए एक 'case nil' शामिल कर सकता है जो बिल्कुल कोई concrete type नहीं रखता, जिसे अलग से करना worth है क्योंकि एक nil interface एक non-nil interface के अंदर stored एक nil pointer से अलग व्यवहार करता है।

उदाहरण: Handling the nil Case

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// Define the function report
func report(i interface{}) {
	// Choose a branch based on the value of i.(type
	switch i.(type) {
	case nil:
		fmt.Println("no value provided")
	case int:
		fmt.Println("got an int")
	// default runs when no case matches
	default:
		fmt.Println("got something else")
	}
}

// main is where the program starts running
func main() {
	report(nil)
	report(7)
}
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. यह भूल जाना कि special switch v := i.(type) syntax सिर्फ सीधे एक switch statement में काम करता है, एक standalone expression के रूप में नहीं।
  2. default case handle न करना, उन types को चुपचाप नज़रअंदाज़ करते हुए जिनकी आपने उम्मीद नहीं की थी।
  3. हर case में shadowed एक जैसे variable name को फिर से उपयोग करना और इसकी type को लेकर confuse होना (यह असल में हर case में सही से re-typed है, यह newcomers को उलझाता है)।
चैप्टर सारांश
  • एक type switch, 'switch v := i.(type) { }', किसी interface value में stored concrete type के आधार पर branch करता है।
  • हर case एक concrete type नाम देता है, और उस case के अंदर, variable की वह specific type होती है।
  • एक default case किसी भी explicitly listed नहीं की गई type को पकड़ता है।
  • Type switches एक interface value में रखी कई संभव concrete types handle करने का idiomatic तरीका हैं।
🔒

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.