Module 6: Interfaces & Error Handling
Interfaces (လုပ်ဆောင်ချက် စည်းမျဉ်းများ)
Section titled “Interfaces (လုပ်ဆောင်ချက် စည်းမျဉ်းများ)”Interface ဆိုတာ “ဒီ Type တစ်ခုမှာ ဘာ Method တွေ ပါရမယ်” လို့ သတ်မှတ်ပေးတဲ့ စည်းမျဉ်း (Contract) တစ်ခုပါ။ Go ရဲ့ Interface တွေက Implicit ဖြစ်ပါတယ်။ ဆိုလိုတာက implements ဆိုတဲ့ keyword သုံးစရာမလိုဘဲ၊ Interface မှာပါတဲ့ Method တွေကို ရေးထားရင် အလိုအလျောက် အဲဒီ Interface ကို လိုက်နာပြီးသား ဖြစ်သွားပါတယ်။
// Shape ဆိုတဲ့ Interface ကို တည်ဆောက်ခြင်းtype Shape interface { Area() float64}
// Circle Structtype Circle struct { Radius float64}
// Circle အတွက် Area Method ရေးခြင်း (Shape Interface ကို အလိုအလျောက် လိုက်နာသွားသည်)func (c Circle) Area() float64 { return 3.14 * c.Radius * c.Radius}
// Rectangle Structtype Rectangle struct { Width, Height float64}
// Rectangle အတွက် Area Method ရေးခြင်းfunc (r Rectangle) Area() float64 { return r.Width * r.Height}
// Interface ကို အသုံးပြုသော Functionfunc printArea(s Shape) { fmt.Println("Area:", s.Area())}
func main() { c := Circle{Radius: 5} r := Rectangle{Width: 4, Height: 5}
printArea(c) // Circle ကို ထည့်လို့ရတယ် printArea(r) // Rectangle ကိုလည်း ထည့်လို့ရတယ်}Error Handling (အမှားများကို ကိုင်တွယ်ခြင်း)
Section titled “Error Handling (အမှားများကို ကိုင်တွယ်ခြင်း)”Go မှာ try-catch လိုမျိုး Exception Handling မရှိပါဘူး။ အဲဒီအစား Error တွေကို ပုံမှန် Return Value တစ်ခုအနေနဲ့ ပြန်ပေးပါတယ်။
import ( "errors" "fmt")
// Error ပြန်ပေးနိုင်သော Functionfunc divide(a, b float64) (float64, error) { if b == 0 { // Error အသစ် ဖန်တီးခြင်း return 0, errors.New("cannot divide by zero") } return a / b, nil // nil ဆိုသည်မှာ Error မရှိပါ ဟုဆိုလိုသည်}
func main() { result, err := divide(10, 0)
// Error ရှိမရှိ စစ်ဆေးခြင်း (Go တွင် အလွန်အသုံးများသော ပုံစံ) if err != nil { fmt.Println("Error:", err) return }
fmt.Println("Result:", result)}