Interface သည် method behavior ကိုသတ်မှတ်တဲ့ contract ပါ။ Go မှာ interface ကို explicit implements လို့ရေးစရာမလိုပါ။ Type တစ်ခုက interface ထဲက methods အားလုံးရှိနေပြီဆိုရင် အလိုအလျောက် satisfy ဖြစ်ပါတယ်။
go
package main
import "fmt"
type Notifier interface {
Notify(message string)
}
type EmailNotifier struct{}
func (EmailNotifier) Notify(message string) {
fmt.Println("Email:", message)
}
func sendAlert(n Notifier) {
n.Notify("Server is running")
}
func main() {
email := EmailNotifier{}
sendAlert(email)
}EmailNotifier မှာ Notify method ရှိတဲ့အတွက် Notifier interface ကို satisfy လုပ်ပါတယ်။ ဒါကြောင့် sendAlert က EmailNotifier ကိုလက်ခံနိုင်တာပါ။
You should see
Email: Server is runningInfo
Interface ကိုသုံးရင် function တစ်ခုက concrete type တစ်မျိုးတည်းမဟုတ်ဘဲ behavior တူတဲ့ type မျိုးစုံနဲ့အလုပ်လုပ်နိုင်ပါတယ်။