Type के Switches
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
// 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)
}
Login to try C/C++/Java/PHP code in the editor
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
// 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))
}
Login to try C/C++/Java/PHP code in the editor
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
// 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)
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि special switch v := i.(type) syntax सिर्फ सीधे एक switch statement में काम करता है, एक standalone expression के रूप में नहीं।
- default case handle न करना, उन types को चुपचाप नज़रअंदाज़ करते हुए जिनकी आपने उम्मीद नहीं की थी।
- हर 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: