Interface के Basics
type Name interface {
Method1(param type) returnType
Method2()
}
Defining an Interface
एक interface type method signatures का एक set list करता है। यह खुद कोई implementation प्रदान नहीं करता -- यह बिल्कुल एक contract है जो describe करता है कि किसी type को क्या करने में सक्षम होना चाहिए।
उदाहरण: Defining an Interface
// 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())
}
Login to try C/C++/Java/PHP code in the editor
Implicit Satisfaction
उन languages के विपरीत जहां किसी type को explicitly declare करना होता है कि वह कौन से interfaces implement करती है, Go अपने आप तय करता है: अगर किसी type के पास किसी interface के signatures से मेल खाते methods हों, यह उस interface को satisfy करती है, बिना किसी declaration की ज़रूरत के।
उदाहरण: Implicit Satisfaction
// 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())
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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())
}
Login to try C/C++/Java/PHP code in the editor
- Java की तरह explicitly 'implements InterfaceName' declare करने की उम्मीद करना -- Go interfaces matching methods से implicitly satisfy होते हैं।
- बहुत सारे methods वाला एक interface define करना, types के लिए इसे satisfy करना मुश्किल बनाते हुए -- idiomatic Go छोटे, केंद्रित interfaces को प्राथमिकता देता है।
- यह मान लेना कि एक 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: