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

खाली Interface

Empty interface, interface{}, बिल्कुल कुछ भी रखने के लिए shaped एक box है, क्योंकि इसके अंदर fit होने के लिए यह zero specific abilities मांगता है।
Syntax
go
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{}?

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

The any Alias

Go 1.18 से, any interface{} के लिए एक built-in alias है, जो arbitrary values accept करने वाले code को पढ़ना आसान बनाने के लिए introduce किया गया, खासकर जब generics ने भी type constraints के लिए interfaces का भारी उपयोग शुरू किया।

Note: नए code में any को प्राथमिकता दें -- यह interface{} के बिल्कुल बराबर है लेकिन ज़्यादा साफ़ पढ़ा जाता है।

उदाहरण: The any Alias

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

A Heterogeneous Slice

चूंकि interface{} कुछ भी accept करता है, यह आपको एक ऐसी slice बनाने देता है जो बिल्कुल अलग types की values रखे, कुछ जो एक normal typed slice जैसे []int नहीं कर सकता।

उदाहरण: A Heterogeneous Slice

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() {
	// 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)
	}
}
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. हर जगह interface{} (या any) का ज़्यादा उपयोग करना, जब एक specific type काम करती तब Go की compile-time type checking को फेंकते हुए।
  2. यह भूल जाना कि किसी empty interface में store की गई value को इसके concrete type के रूप में असल में उपयोग करने के लिए एक type assertion या type switch चाहिए।
  3. 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:

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.