खाली Interface
In this page:
var name interface{} // or: var name any
name = value
What Is interface{}?
interface{} बिल्कुल बिना किसी method requirements वाला एक interface है, जिसका मतलब है Go में literally हर type इसे अपने आप satisfy करती है। यही वह तरीका है जिससे fmt.Println जैसे functions किसी भी type के arguments accept कर सकते हैं।
उदाहरण: What Is interface{}?
// 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(v interface{}) {
// Print using a format string
fmt.Printf("value: %v, type: %T\n", v, v)
}
// main is where the program starts running
func main() {
describe(42)
describe("hello")
describe(true)
}
Login to try C/C++/Java/PHP code in the editor
The any Alias
Go 1.18 से, any interface{} के लिए एक built-in alias है, जो arbitrary values accept करने वाले code को पढ़ना आसान बनाने के लिए introduce किया गया, खासकर जब generics ने भी type constraints के लिए interfaces का भारी उपयोग शुरू किया।
उदाहरण: The any Alias
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define the function printAny
func printAny(v interface{}) {
// Print the values followed by a newline
fmt.Println("got:", v)
}
// main is where the program starts running
func main() {
printAny(3.14)
printAny([]int{1, 2, 3})
}
Login to try C/C++/Java/PHP code in the editor
A Heterogeneous Slice
चूंकि interface{} कुछ भी accept करता है, यह आपको एक ऐसी slice बनाने देता है जो बिल्कुल अलग types की values रखे, कुछ जो एक normal typed slice जैसे []int नहीं कर सकता।
उदाहरण: A Heterogeneous Slice
// 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() {
// Short declaration: create mixed and infer the type
mixed := []interface{}{1, "two", 3.0, false}
// Loop over mixed; the blank identifier _ ignores the index
for _, v := range mixed {
// Print the values followed by a newline
fmt.Println(v)
}
}
Login to try C/C++/Java/PHP code in the editor
- हर जगह interface{} (या any) का ज़्यादा उपयोग करना, जब एक specific type काम करती तब Go की compile-time type checking को फेंकते हुए।
- यह भूल जाना कि किसी empty interface में store की गई value को इसके concrete type के रूप में असल में उपयोग करने के लिए एक type assertion या type switch चाहिए।
- any और 'interface{}' को अलग चीज़ें मानना -- any Go 1.18 से बस interface{} का एक alias है।
- interface{} (Go 1.18 से any के रूप में aliased) की कोई method requirements नहीं, इसलिए हर type इसे satisfy करती है।
- इसका उपयोग तब होता है जब किसी function को genuinely किसी भी type की value accept करनी हो, जैसे fmt.Println के arguments।
- concrete value वापस पाने के लिए एक type assertion या type switch चाहिए।
- empty interface का ज़्यादा उपयोग Go की compile-time type safety की कुर्बानी देता है, इसलिए इसे कम उपयोग करना चाहिए।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: