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

Interface के Basics

एक interface एक job description जैसा है -- यह list करता है कि किसी चीज़ को कौन से actions करने में सक्षम होना चाहिए, और कोई भी type जो वे सभी कर सके job के लिए qualify करती है, कोई paperwork ज़रूरी नहीं।
Syntax
go
type Name interface {
    Method1(param type) returnType
    Method2()
}

Defining an Interface

एक interface type method signatures का एक set list करता है। यह खुद कोई implementation प्रदान नहीं करता -- यह बिल्कुल एक contract है जो describe करता है कि किसी type को क्या करने में सक्षम होना चाहिए।

उदाहरण: Defining an Interface

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

// Import the fmt package
import "fmt"

// Define an interface named Shape (a set of method signatures)
type Shape interface {
	Area() float64
}

// Define a struct type named Circle
type Circle struct {
	Radius float64
}

// Define the method Area on type Circle
func (c Circle) Area() float64 {
	// Send 3.14159 * c.Radius * c.Radius back to the caller
	return 3.14159 * c.Radius * c.Radius
}

// main is where the program starts running
func main() {
	// Declare Shape and set it to Circle{Radius: 2}
	var s Shape = Circle{Radius: 2}
	fmt.Println(s.Area())
}

Implicit Satisfaction

उन languages के विपरीत जहां किसी type को explicitly declare करना होता है कि वह कौन से interfaces implement करती है, Go अपने आप तय करता है: अगर किसी type के पास किसी interface के signatures से मेल खाते methods हों, यह उस interface को satisfy करती है, बिना किसी declaration की ज़रूरत के।

Note: यह implicit design मतलब है कि आप एक ऐसा interface define कर सकते हैं जिसे मौजूदा, unmodified types पहले से satisfy करती हों।

उदाहरण: Implicit Satisfaction

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

// Import the fmt package
import "fmt"

// Define an interface named Greeter (a set of method signatures)
type Greeter interface {
	Greet() string
}

// Define a struct type named English
type English struct{}

func (English) Greet() string { return "Hello" }

// Define a struct type named Spanish
type Spanish struct{}

func (Spanish) Greet() string { return "Hola" }

// main is where the program starts running
func main() {
	// Short declaration: create greeters and infer the type
	greeters := []Greeter{English{}, Spanish{}}
	// Loop over greeters; the blank identifier _ ignores the index
	for _, g := range greeters {
		fmt.Println(g.Greet())
	}
}

Why Small Interfaces Win

चूंकि किसी interface को satisfy करने के लिए सिर्फ सही methods चाहिए, छोटे interfaces (अक्सर एक single method) को कई अलग types के लिए satisfy करना आसान है, code को ज़्यादा flexible और reusable बनाते हुए -- यही वजह है कि io.Reader और io.Writer, हर एक एक method के साथ, Go की standard library के इतने केंद्रीय हैं।

उदाहरण: Why Small Interfaces Win

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

// Import the fmt package
import "fmt"

// Define an interface named Stringer (a set of method signatures)
type Stringer interface {
	String() string
}

type Money int

// Define the method String on type Money
func (m Money) String() string {
	// Sprintf builds a formatted string instead of printing it
	return fmt.Sprintf("$%d", m)
}

// main is where the program starts running
func main() {
	// Declare Stringer and set it to Money(50)
	var s Stringer = Money(50)
	// Print the values followed by a newline
	fmt.Println(s.String())
}
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. Java की तरह explicitly 'implements InterfaceName' declare करने की उम्मीद करना -- Go interfaces matching methods से implicitly satisfy होते हैं।
  2. बहुत सारे methods वाला एक interface define करना, types के लिए इसे satisfy करना मुश्किल बनाते हुए -- idiomatic Go छोटे, केंद्रित interfaces को प्राथमिकता देता है।
  3. यह मान लेना कि एक concrete type सेट वाली एक interface value हर case में == से उस concrete value के बराबर compare होती है, nil interface की सूक्ष्मताओं पर विचार किए बिना।
चैप्टर सारांश
  • एक interface method signatures का एक set define करता है जो किसी type को इसे satisfy करने के लिए implement करना चाहिए।
  • Go interfaces implicitly satisfy होते हैं -- कोई implements keyword नहीं है।
  • सही methods वाली कोई भी type अपने आप एक interface satisfy करती है, यहां तक कि वे भी जो आपने नहीं लिखीं।
  • Idiomatic Go छोटे interfaces को प्राथमिकता देता है, अक्सर सिर्फ एक या दो methods के साथ।
🔒

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.