ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Struct ကို ဖောင်ပုံစံ တစ်ခုလို့ မြင်ကြည့်ပါ။ အသင်းဝင် ဖောင်မှာ "နာမည်"၊ "email"၊ "အသက်" ဆိုတဲ့ အကွက်တွေ ရှိပြီး အကွက်တစ်ခုစီမှာ ဘာအမျိုးအစား ဖြည့်ရမလဲ သတ်မှတ်ထားပါတယ်။ type User struct က ဖောင်ပုံစံ ဖြစ်ပြီး User{Name: "Aung", ...} က ဖြည့်ပြီးသား ဖောင် တစ်စောင်ပါ။
Variable သုံးခု (name, email, age) ကို သီးခြားစီ ကိုင်ထားမယ့်အစား User တစ်ခုတည်းကို function တွေဆီ ပို့၊ slice ထဲ ထည့်၊ JSON အဖြစ် ပြောင်းလို့ ရပါတယ်။ Go မှာ class မရှိပေမယ့် struct နဲ့ နောက် lesson မှာ လေ့လာမယ့် method တွေက အဲဒီ နေရာကို ယူပါတယ်။
Problem → Why → Solution
ပြဿနာ: field အစဉ် မှားပြီး ဘယ်သူမှ မသိဘူး
Struct literal ကို field name မပါဘဲ အစဉ်လိုက် ရေးလို့ ရပါတယ် — User{"Aung", "aung@example.com", 25} ။ ဒါပေမယ့် Name နဲ့ Email နှစ်ခုလုံး string ဖြစ်လို့ အစဉ် ပြောင်းပြန် ရေးမိရင် compiler က ဘာမှ မပြောပါဘူး။ Email ပို့တဲ့ system က "Aung" ဆီ mail ပို့ဖို့ ကြိုးစားပြီး ပြဿနာက production ရောက်မှ ပေါ်ပါတယ်။
package main
import "fmt"
type User struct {
Name string
Email string
Age int
}
func main() {
u := User{"aung@example.com", "Aung", 25} // compiles: both are strings
fmt.Println("sending mail to", u.Email) // sending mail to Aung
}Field name ပါတဲ့ literal က ဘာကြောင့် ပိုကောင်းလဲ
Positional literal က field အစဉ်ကို မှီခိုလို့ type တူတဲ့ field နှစ်ခု လဲသွားတာကို ဖမ်းမပေးနိုင်ပါဘူး။ နောက်ပြီး struct ထဲကို field အသစ် ထည့်လိုက်တာနဲ့ positional literal တိုင်း "too few values in struct literal" error ဖြစ်ပါတယ်။ User{Name: "Aung", Email: "aung@example.com"} လို keyed literal က အစဉ်ကို မမှီခိုဘဲ မထည့်တဲ့ field ကို zero value ပေးလို့ နှစ်ခုလုံးကို ကာကွယ်ပေးပါတယ်။
Core concept: field တွေ စုထားတဲ့ value type
type User struct { Name string; Email string; Age int } က User ဆိုတဲ့ type အသစ်ကို ကြေညာပါတယ်။ Field တစ်ခုချင်းကို user.Name လို dot နဲ့ ဖတ်၊ ရေးပါတယ်။ var guest User လို ကြေညာရုံဆိုရင် field တိုင်းက zero value ဖြစ်ပါတယ် — Name က ""၊ Age က 0 ပါ။ fmt.Printf("%+v", guest) က field name တွေပါ ပြလို့ debug လုပ်တဲ့အခါ အသုံးဝင်ပါတယ်။ Make လုပ်ရမယ့် map field လို သုံးမီ ပြင်ဆင်စရာ ရှိတဲ့ struct တွေအတွက် Go code မှာ NewUser လို နာမည်ရှိတဲ့ constructor function တစ်ခု ရေးပြီး အသင့်သုံးလို့ရတဲ့ value ကို ပြန်ပေးလေ့ ရှိပါတယ်။
Array လိုပဲ struct ကလည်း value type ပါ။ copyUser := user က field အားလုံးကို copy ကူးလို့ copyUser.Age ကို ပြောင်းတာက user ကို မထိခိုက်ပါဘူး။ Function ဆီ struct ပို့ရင်လည်း copy ရပါတယ်။ မူလ struct ကို ပြင်ချင်ရင် pointer ကို သုံးပါ — p := &user ပြီးနောက် p.Age++ ဆိုရင် Go က (*p).Age ကို အလိုအလျောက် လုပ်ပေးလို့ user.Age ကိုယ်တိုင် ပြောင်းပါတယ်။
Field အားလုံး comparable ဖြစ်ရင် struct နှစ်ခုကို == နဲ့ field တစ်ခုချင်း နှိုင်းယှဉ်လို့ ရပြီး map key အဖြစ်လည်း သုံးနိုင်ပါတယ်။ Slice, map ဒါမှမဟုတ် function field တစ်ခု ပါတာနဲ့ == က "struct containing []string cannot be compared" compile error ဖြစ်ပြီး အဲဒီအခါ slices.Equal လို function နဲ့ ကိုယ်တိုင် နှိုင်းယှဉ်ရပါတယ်။
Field name ကို uppercase နဲ့ စရင် exported ဖြစ်ပြီး တခြား package ကနေ မြင်ရပါတယ်၊ lowercase ဆိုရင် ကိုယ့် package အတွင်းမှာပဲ မြင်ရပါတယ်။ encoding/json ကလည်း exported field တွေကိုပဲ encode လုပ်ပါတယ်။ Struct တစ်ခုကို နောက် struct ထဲမှာ field name မပေးဘဲ ထည့်တာကို embedding လို့ ခေါ်ပြီး အထဲက field တွေက customer.City လို အပြင်ကို promote ဖြစ်လာပါတယ်။ ဒါက inheritance မဟုတ်ဘဲ composition ပါ — Customer ထဲမှာ Address တစ်ခု ရှိနေတာပဲ ဖြစ်ပါတယ်။
- type က field တွေ စုထားတဲ့ ပုံစံ
- Literal ဒါမှမဟုတ် zero value နဲ့ ဖန်တီး
- = နဲ့ ပို့ရင် ဖောင်တစ်စောင်လုံး copy
- &value ပို့မှ မူလကို ပြင်နိုင်
Struct ကို ပေးလိုက်တိုင်း မိတ္တူ ပေးတာပါ၊ မူရင်းဖောင်ကို ပြင်စေချင်ရင် pointer ဆိုတဲ့ လိပ်စာကို ပေးရပါတယ်။
အထွေထွေ ပုံစံ
type TypeName struct {
ExportedField FieldType
unexportedField FieldType
EmbeddedType // fields are promoted
}
value := TypeName{ExportedField: x} // omitted fields get zero values
var empty TypeName // every field is a zero value
value.ExportedField = y
ptr := &value
ptr.ExportedField = z // same as (*ptr).ExportedFieldField name ပါတဲ့ literal ကို အမြဲ သုံးပါ။ Uppercase field တွေက package အပြင်ကို export ဖြစ်ပြီး embed လုပ်ထားတဲ့ type ရဲ့ field တွေကို တိုက်ရိုက် ခေါ်လို့ ရပါတယ်။ Pointer ကနေ field ကို dot နဲ့ တိုက်ရိုက် ဖတ်၊ ရေးလို့ ရပါတယ်။
Example: User struct နဲ့ copy, pointer, ==
ဒီ program က User struct ကို keyed literal နဲ့ ဖန်တီးပြီး field တွေ ဖတ်တာ၊ zero value User ကို %+v နဲ့ print ထုတ်တာ၊ copy ကို ပြင်တာနဲ့ pointer ကနေ မူလကို ပြင်တာ၊ နောက်ဆုံးမှာ == နဲ့ နှိုင်းယှဉ်တာကို ပြပါတယ်။ "25 30" line နဲ့ "26 true" line ကို ဘာကြောင့် ဒီလို ထွက်လဲ စဉ်းစားကြည့်ပါ။
package main
import "fmt"
type User struct {
Name string
Email string
Age int
}
func main() {
user := User{Name: "Aung", Email: "aung@example.com", Age: 25}
fmt.Println(user.Name)
fmt.Println(user.Email)
var guest User
fmt.Printf("%+v\n", guest)
copyUser := user
copyUser.Age = 30
fmt.Println(user.Age, copyUser.Age)
p := &user
p.Age++
fmt.Println(user.Age, user == User{Name: "Aung", Email: "aung@example.com", Age: 26})
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Type declaration
type User struct {Name, Email, Age ဆိုတဲ့ field သုံးခုပါတဲ့ type အသစ်ပါ။ Uppercase နဲ့ စလို့ field သုံးခုလုံး exported ဖြစ်ပါတယ်။
- Keyed literal
user := User{Name: "Aung", Email: "aung@example.com", Age: 25}Field name တွေနဲ့ value ထည့်လို့ အစဉ်မှားဖို့ မရှိပါဘူး။ user.Name နဲ့ user.Email က Aung နဲ့ aung@example.com ကို print ထုတ်ပါတယ်။
- Zero value
fmt.Printf("%+v\n", guest)guest ကို ကြေညာရုံပဲ လုပ်ထားလို့ string field တွေ ဗလာ၊ Age က 0 ဖြစ်ပြီး {Name: Email: Age:0} လို့ ထွက်ပါတယ်။
- Copy
copyUser := userField အားလုံးကို copy ကူးလို့ copyUser.Age = 30 က copy ကိုပဲ ပြောင်းပြီး "25 30" ထွက်ပါတယ်။
- Pointer
p := &userp က user ရဲ့ လိပ်စာပါ။ p.Age++ က (*p).Age++ နဲ့ အတူတူဖြစ်ပြီး user.Age ကိုယ်တိုင် 26 ဖြစ်သွားပါတယ်။
- Comparison
user == User{Name: "Aung", Email: "aung@example.com", Age: 26}Field သုံးခုလုံး string နဲ့ int ဖြစ်လို့ comparable ပါ။ Field တိုင်း တူလို့ true ရပါတယ်။
Aung
aung@example.com
{Name: Email: Age:0}
25 30
26 trueEmbedding၊ struct tag နဲ့ anonymous struct
Customer ထဲမှာ Address ကို field name မပေးဘဲ embed လုပ်ထားလို့ c.City လို တိုက်ရိုက် ဖတ်လို့ ရပြီး c.Address.City လို အပြည့်အစုံ ရေးလည်း ရပါတယ်။ Backtick ထဲက json:"name" လို struct tag က encoding/json ကို JSON key နာမည် ပြောပြပါတယ်။ Lowercase note field ကတော့ unexported ဖြစ်လို့ JSON ထဲ မပါပါဘူး။ နောက်ဆုံး line က နာမည်မပေးတဲ့ anonymous struct ဖြစ်ပြီး တစ်ခါသုံး data အတွက် အဆင်ပြေပါတယ်။
package main
import (
"encoding/json"
"fmt"
)
type Address struct {
City string `json:"city"`
}
type Customer struct {
Name string `json:"name"`
Address // embedded: City is promoted
Points int `json:"points"`
note string // unexported: json skips it
}
func main() {
c := Customer{Name: "Su", Address: Address{City: "Yangon"}, Points: 120, note: "vip"}
fmt.Println(c.Name, c.City, c.Address.City, c.Points, c.note) // Su Yangon Yangon 120 vip
data, _ := json.Marshal(c)
fmt.Println(string(data)) // {"name":"Su","city":"Yangon","points":120}
point := struct{ X, Y int }{3, 4}
fmt.Printf("%+v\n", point) // {X:3 Y:4}
}လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Go web service တစ်ခုမှာ struct တွေက data ရဲ့ ပုံသဏ္ဌာန် အားလုံးကို ကိုယ်စားပြုပါတယ် — HTTP request body ကို decode လုပ်ဖို့ CreateUserRequest၊ database row တစ်ခုကို ကိုယ်စားပြုတဲ့ User၊ API response အတွက် UserResponse စတာတွေပါ။ json:"email" လို tag တွေနဲ့ Go ရဲ့ Email field ကို JSON ရဲ့ email key နဲ့ ချိတ်ပေးပြီး field ကို lowercase ထားရင် JSON ထဲ မပါတော့လို့ password hash လို data ကို response ထဲ မပေါက်ကြားအောင် သီးခြား response struct ကို သုံးလေ့ ရှိပါတယ်။
Embedding ကို field တွေ ထပ်ခါထပ်ခါ မရေးရအောင် သုံးပါတယ်၊ ဥပမာ ID, CreatedAt, UpdatedAt ပါတဲ့ Model struct ကို Product, Order ထဲမှာ embed လုပ်တာမျိုးပါ။ Struct ကြီးတွေ (field များစွာ ပါတာ) ကို slice ထဲမှာ သိမ်းပြီး loop ထဲမှာ ပြင်ရမယ်ဆိုရင် copy ကို ပြင်မိတာ ဖြစ်တတ်လို့ index နဲ့ users[i] ကို ပြင်တာ ဒါမှမဟုတ် pointer slice ([]*User) ကို သုံးတာကို သတိထားပါ။ Comparable struct တွေကို map key အဖြစ် သုံးတာ — map[Point]bool လို — ကလည်း grid နဲ့ coordinate data အတွက် အသုံးများပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Lowercase field ကို export ဖြစ်မယ်လို့ ထင်တာ
type User struct { name string } ကို တခြား package ကနေ u.name လို ဖတ်ရင် compile error ဖြစ်ပြီး json.Marshal(u) ကလည်း {} ပဲ ထုတ်ပါတယ်။ Error တစ်ခုမှ မပြဘဲ data ပျောက်သွားတာမို့ အန္တရာယ်များပါတယ်။ Package အပြင်က မြင်ရမယ့် field တွေကို uppercase နဲ့ စပြီး JSON key ကို lowercase ချင်ရင် tag ကို သုံးပါ။
2. Function ထဲမှာ struct ပြင်ပြီး caller မှာ ပြောင်းမယ်လို့ ထင်တာ
func birthday(u User) { u.Age++ } က copy ရဲ့ Age ကိုပဲ တိုးလို့ caller ရဲ့ user.Age မပြောင်းပါဘူး။ func birthday(u *User) { u.Age++ } လို pointer ကို လက်ခံပြီး birthday(&user) နဲ့ ခေါ်ပါ။
3. Slice field ပါတဲ့ struct ကို == နဲ့ နှိုင်းယှဉ်တာ
Team struct ထဲမှာ Members []string ပါတာနဲ့ a == b က "struct containing []string cannot be compared" compile error ဖြစ်ပါတယ်၊ ဒီ type ကို map key အဖြစ်လည်း သုံးလို့ မရတော့ပါဘူး။ Name ကို == နဲ့၊ Members ကို slices.Equal နဲ့ သီးခြား နှိုင်းယှဉ်တဲ့ function တစ်ခု ရေးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Struct literal တွေမှာ field name ကို အမြဲ ရေးပါ၊ field အသစ် ထည့်တဲ့အခါ code ဟောင်းတွေ မကျိုးတော့ပါဘူး။
- Zero value ကို အသုံးဝင်အောင် design လုပ်ပါ — var cfg Config က ချက်ချင်း သုံးလို့ရရင် constructor မလိုတော့ပါဘူး။
- ငွေကြေးကို float64 မဟုတ်ဘဲ PriceCents int လို integer နဲ့ သိမ်းပြီး rounding အမှားတွေကို ရှောင်ပါ။
- Field များတဲ့ struct ကြီးတွေကို ခဏခဏ ပြင်ရမယ်ဆိုရင် pointer နဲ့ ပို့ပြီး သေးငယ်တဲ့ value struct တွေကိုတော့ copy နဲ့ပဲ ပို့ပါ။
Mini challenge
Mini challenge: Product နဲ့ order line
Product struct နဲ့ Product ကို embed လုပ်ထားတဲ့ LineItem struct ကို ရေးပြီး copy နဲ့ pointer ကွာခြားချက်ကို output ကနေ ပြပါ။ Method တွေကို နောက် lesson မှာ လေ့လာမှာမို့ ဒီမှာ ရိုးရိုး function တွေပဲ သုံးပါ။
- Product မှာ Name string နဲ့ PriceCents int ပါရမယ် (ငွေကို cent နဲ့ int အဖြစ် သိမ်း)
- LineItem က Product ကို embed လုပ်ပြီး Qty int ပါရမယ်၊ item.Name လို promoted field ကို သုံးရမယ်
- lineTotal(item LineItem) int နဲ့ discount(p *Product, percent int) function နှစ်ခု ရေးရမယ်
- tea ကို discount လုပ်ပြီးနောက် item ထဲက copy မပြောင်းကြောင်းနဲ့ == ရလဒ်ကို print ထုတ်ရမယ်
package main
import "fmt"
type Product struct {
// TODO: Name string, PriceCents int
}
type LineItem struct {
// TODO: embed Product, add Qty int
}
func lineTotal(item LineItem) int {
// TODO: price times quantity, using the promoted field
return 0
}
func discount(p *Product, percent int) {
// TODO: lower p.PriceCents by percent
}
func main() {
// TODO: tea costs 250 cents; build a LineItem with Qty 4
// TODO: print name and total, discount tea by 20,
// print tea's price and the item's price, then compare with ==
fmt.Println("todo")
}ရလဒ်: Tea 1000 200 250 true
အရိပ်အမြွက် ကြည့်ရန်
LineItem{Product: tea, Qty: 4} က tea ကို item ထဲ copy ကူးပါတယ်။ ဒါကြောင့် discount(&tea, 20) ပြီးနောက် tea.PriceCents က 200 ဖြစ်ပေမယ့် item.PriceCents က 250 အတိုင်း ကျန်ပါတယ်။
အဖြေ ကြည့်ရန်
package main
import "fmt"
type Product struct {
Name string
PriceCents int
}
type LineItem struct {
Product
Qty int
}
func lineTotal(item LineItem) int {
return item.PriceCents * item.Qty
}
func discount(p *Product, percent int) {
p.PriceCents -= p.PriceCents * percent / 100
}
func main() {
tea := Product{Name: "Tea", PriceCents: 250}
item := LineItem{Product: tea, Qty: 4}
fmt.Println(item.Name, lineTotal(item))
discount(&tea, 20)
fmt.Println(tea.PriceCents, item.PriceCents)
fmt.Println(tea == Product{Name: "Tea", PriceCents: 200})
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- type Name struct { ... } က field တွေကို type တစ်ခုအဖြစ် စုပြီး keyed literal နဲ့ ဖန်တီးတာ အလုံခြုံဆုံးပါ။
- Struct က value type ဖြစ်လို့ assign လုပ်တာ၊ function ဆီ ပို့တာက copy ဖြစ်ပြီး pointer (&value) ကနေမှ မူလကို ပြင်နိုင်ပါတယ်။
- Field အားလုံး comparable ဖြစ်မှ == နဲ့ map key အဖြစ် သုံးလို့ ရပြီး slice field တစ်ခု ပါတာနဲ့ မရတော့ပါဘူး။
- Uppercase field တွေပဲ export ဖြစ်ပြီး JSON ထဲ ပါပါတယ်၊ embedding က field တွေကို promote လုပ်ပေးတဲ့ composition ပါ။
A Tour of Go — Go