ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Array ကို ကြက်ဥ ခြောက်လုံးထည့် ဗူးလေးလို့ မြင်ကြည့်ပါ။ အကွက်အရေအတွက်က ဗူးလုပ်ကတည်းက သတ်မှတ်ပြီးသား ဖြစ်ပြီး နောက်မှ အကွက်ထပ်တိုးလို့ မရပါဘူး။ အကွက်တိုင်းမှာ နံပါတ်ရှိပြီး 0 ကနေ စပါတယ်၊ ဒါကြောင့် ပထမ ကြက်ဥက အကွက် 0 မှာ ရှိပါတယ်။
Go မှာ ဗူးရဲ့ အရွယ်အစားက ဗူးရဲ့ အမျိုးအစား (type) ရဲ့ အစိတ်အပိုင်း ဖြစ်ပါတယ် — ခြောက်လုံးထည့်ဗူးနဲ့ ဆယ့်နှစ်လုံးထည့်ဗူးက မတူတဲ့ ပစ္စည်းတွေပါ။ နောက်ထူးခြားချက်တစ်ခုက ဗူးကို တစ်ယောက်ယောက်ကို ပေးလိုက်ရင် ဗူးအသစ်တစ်လုံး ကူးပေးလိုက်တာ ဖြစ်တာပါ။
Problem → Why → Solution
ပြဿနာ: function ထဲမှာ ပြောင်းလိုက်ပေမယ့် မပြောင်းဘူး
JavaScript ဒါမှမဟုတ် Python ကနေ လာတဲ့သူတွေက array ကို function ဆီ ပို့ရင် function က မူလ data ကို တိုက်ရိုက် ပြင်မယ်လို့ မျှော်လင့်ကြပါတယ်။ အောက်က resetFirst() က ပထမ အမှတ်ကို 0 ပြောင်းပါတယ်။ ဒါပေမယ့် main() က scores ကို print ထုတ်တော့ [80 90 100] ပဲ ပြန်ထွက်လာပြီး error တစ်ခုမှ မပေါ်ပါဘူး။
package main
import "fmt"
func resetFirst(s [3]int) {
s[0] = 0 // changes the function's own copy
}
func main() {
scores := [3]int{80, 90, 100}
resetFirst(scores)
fmt.Println(scores) // [80 90 100], not [0 90 100]
}ဘာကြောင့် မပြောင်းတာလဲ
Go မှာ array က value တစ်ခုပါ — int တစ်ခုလိုပဲ ဆက်ဆံပါတယ်။ resetFirst(scores) လို့ ခေါ်လိုက်တဲ့အချိန်မှာ element သုံးခုလုံးကို s ထဲ copy ကူးပေးလိုက်ပြီး function က အဲဒီ copy ကိုပဲ ပြင်ပါတယ်။ Caller ရဲ့ array ကို ပြင်ချင်ရင် ပြင်ပြီးသား array ကို return ပြန်ရမယ်၊ ဒါမှမဟုတ် pointer (*[3]int) ပို့ရပါမယ်။
Core concept: length က type ရဲ့ အစိတ်အပိုင်း၊ array က value
Array type ကို [N]T လို့ ရေးပါတယ် — N က element အရေအတွက်၊ T က element type ပါ။ N က constant ဖြစ်ရပြီး type ရဲ့ အစိတ်အပိုင်း ဖြစ်လို့ [3]int နဲ့ [4]int က လုံးဝ မတူတဲ့ type နှစ်ခုပါ။ [3]int variable ကို [4]int variable ထဲ assign လုပ်ရင် "cannot use scores (variable of type [3]int) as [4]int value" compile error ရပါတယ်။ Length ကို run နေတုန်း ပြောင်းလို့လည်း မရပါဘူး။
Array ကို ဖန်တီးနည်း သုံးမျိုး ရှိပါတယ်။ [3]int{80, 90, 100} က literal ပါ။ [...]int{2, 3, 5} လို ရေးရင် compiler က element ရေတွက်ပြီး length ကို ဖြည့်ပေးပါတယ်။ var empty [3]int လို ကြေညာရုံပဲ ဆိုရင် element တိုင်းက zero value ဖြစ်ပါတယ် — int အတွက် 0၊ string အတွက် ""၊ bool အတွက် false ပါ။ Go array မှာ "မထည့်ရသေးတဲ့ အကွက်" ဆိုတာ မရှိပါဘူး။ [5]string{1: "B", 3: "D"} လို ရွေးထားတဲ့ နေရာတွေကိုပဲ ဖြည့်လို့လည်း ရပြီး ကျန်တဲ့ အကွက်တွေက zero value ဖြစ်ပါတယ်။
အရေးကြီးဆုံးအချက်က array က value ဖြစ်တာပါ။ backup := scores လို assign လုပ်ရင် element အားလုံး copy ကူးပြီး backup ကို ပြင်တာက scores ကို မထိခိုက်ပါဘူး။ Function ဆီ ပို့တာ၊ return ပြန်တာမှာလည်း copy ပါပဲ။ Element type က comparable ဖြစ်ရင် array နှစ်ခုကို == နဲ့ element တစ်ခုချင်း နှိုင်းယှဉ်လို့ ရပြီး map key အဖြစ်လည်း သုံးလို့ ရပါတယ်။
Index ကို 0 ကနေ len(a)-1 အထိပဲ သုံးလို့ ရပါတယ်။ scores[3] လို constant index ကျော်သွားရင် compiler က "index 3 out of bounds [0:3]" လို့ ဖမ်းပေးပါတယ်။ Index က run နေတုန်း တွက်ထုတ်တဲ့ variable ဖြစ်ရင်တော့ compiler မသိနိုင်လို့ runtime မှာ "index out of range [3] with length 3" panic ဖြစ်ပြီး program ရပ်သွားပါတယ်။
- [N]T က N အကွက် ပုံသေ ဖန်တီး
- အကွက်တိုင်း zero value နဲ့ စ
- = နဲ့ ပို့တာတိုင်း ဗူးတစ်လုံးလုံး copy
- Index 0..N-1 ပြင်ပ = error/panic
Array ကို int တစ်ခုလို ဆက်ဆံပါ — ပို့လိုက်တိုင်း ကိုယ်ပိုင် copy တစ်ခု ထွက်သွားပါတယ်။
အထွေထွေ ပုံစံ
var name [N]Type // N zero values
name := [N]Type{v0, v1, v2} // literal
name := [...]Type{v0, v1} // compiler counts N
name := [N]Type{index: value} // set chosen slots
value := name[i] // read, 0 <= i < N
name[i] = value // write
count := len(name) // always NLength N ကို type ထဲမှာ ရေးရပြီး constant ဖြစ်ရပါမယ်။ [...] က literal ထဲက element အရေအတွက်ကို length အဖြစ် ယူပါတယ်။ index: value ပုံစံက ရွေးထားတဲ့ အကွက်တွေကိုပဲ value ထည့်ပြီး ကျန်တာ zero value ဖြစ်ပါတယ်။
Example: အမှတ် array နဲ့ copy လုပ်ပုံ
ဒီ program က အမှတ်သုံးခု ပါတဲ့ array တစ်ခု ဖန်တီးပြီး index နဲ့ ဖတ်တာ၊ len() သုံးတာ၊ zero value array၊ assignment က copy ဖြစ်ပုံနဲ့ == နဲ့ နှိုင်းယှဉ်တာကို တစ်ဆင့်ချင်း ပြပါတယ်။ "backup" ကို ပြင်ပြီးနောက် scores ဘာဖြစ်လဲ ဆိုတာကို run မလုပ်ခင် ခန့်မှန်းကြည့်ပါ။
package main
import "fmt"
func main() {
scores := [3]int{80, 90, 100}
fmt.Println(scores)
fmt.Println("first score:", scores[0])
fmt.Println("total items:", len(scores))
var empty [3]int
fmt.Println("zero value:", empty)
backup := scores
backup[0] = 0
fmt.Println("scores:", scores, "backup:", backup)
fmt.Println("equal?", scores == [3]int{80, 90, 100})
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Literal
scores := [3]int{80, 90, 100}Type က [3]int ပါ။ Value သုံးခု ထည့်ထားလို့ အကွက်တိုင်း ပြည့်နေပါတယ်။ Value လေးခု ထည့်ရင် compile error ရပါတယ်။
- Index and length
fmt.Println("first score:", scores[0])ပထမ element က index 0 မှာ ရှိလို့ 80 ထွက်ပါတယ်။ နောက် line က len(scores) က type ကနေ သိပြီးသား 3 ကို ပြန်ပေးပါတယ်။
- Zero value
var empty [3]intValue မပေးဘဲ ကြေညာလို့ int ရဲ့ zero value ဖြစ်တဲ့ 0 သုံးခု ရပြီး [0 0 0] လို့ print ထွက်ပါတယ်။
- Copy on assignment
backup := scoresElement သုံးခုလုံးကို backup ထဲ copy ကူးပါတယ်။ ဒါကြောင့် backup[0] = 0 က backup ကိုပဲ ပြောင်းပြီး output မှာ scores က [80 90 100]၊ backup က [0 90 100] ဖြစ်ပါတယ်။
- Comparison
scores == [3]int{80, 90, 100}Type တူတဲ့ array နှစ်ခုကို element တစ်ခုချင်း နှိုင်းယှဉ်ပြီး အကုန်တူလို့ true ရပါတယ်။ [4]int နဲ့ နှိုင်းယှဉ်ရင် type မတူလို့ compile error ပါ။
[80 90 100]
first score: 80
total items: 3
zero value: [0 0 0]
scores: [80 90 100] backup: [0 90 100]
equal? trueCopy နဲ့ pointer၊ indexed literal နဲ့ 2D array
Function က caller ရဲ့ array ကို တကယ် ပြင်ဖို့ လိုရင် *[3]int pointer ကို ပို့ပါ — &nums နဲ့ ခေါ်ပြီး function ထဲမှာ a[0] လို ရေးရင် Go က pointer ကို အလိုအလျောက် dereference လုပ်ပေးပါတယ်။ Array ကြီးတွေ (ဥပမာ [100000]int) ကို value အနေနဲ့ ပို့ရင် ခေါ်တိုင်း copy ကူးရလို့ နှေးနိုင်တာကိုလည်း သတိထားပါ။ Indexed literal နဲ့ array ထဲမှာ array ထည့်တဲ့ grid ပုံစံကိုလည်း ဒီ snippet မှာ ကြည့်လို့ ရပါတယ်။
package main
import "fmt"
func resetCopy(a [3]int) {
a[0] = 0
}
func resetReal(a *[3]int) {
a[0] = 0
}
func main() {
nums := [3]int{7, 8, 9}
resetCopy(nums)
fmt.Println(nums) // [7 8 9]
resetReal(&nums)
fmt.Println(nums) // [0 8 9]
primes := [...]int{2, 3, 5, 7}
slots := [5]string{1: "B", 3: "D"}
fmt.Printf("%d %d %q\n", len(primes), len(slots), slots) // 4 5 ["" "B" "" "D" ""]
var board [3][3]string
board[1][1] = "X"
fmt.Printf("%q\n", board[1]) // ["" "X" ""]
}လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Data အရေအတွက် ပြောင်းနိုင်တဲ့ နေရာ (user list၊ search result) တွေမှာ Go programmer တွေက slice ကို သုံးကြပါတယ်၊ ဒါကို နောက် lesson မှာ လေ့လာပါမယ်။ Array ကတော့ အရွယ်အစားက ဒေတာရဲ့ အဓိပ္ပာယ် ဖြစ်နေတဲ့ နေရာတွေမှာ ပေါ်ပါတယ်။ crypto/sha256 ရဲ့ sha256.Sum256(data) က [32]byte ကို ပြန်ပေးပါတယ် — SHA-256 hash က 32 byte အမြဲ ဖြစ်လို့ပါ။ RGB color ကို [3]uint8၊ IPv4 address ကို [4]byte၊ tic-tac-toe board ကို [3][3]string လို ရေးတာမျိုးပါ။
Array က comparable ဖြစ်လို့ map key အဖြစ် သုံးနိုင်တာက လက်တွေ့မှာ အသုံးဝင်ပါတယ်။ ဥပမာ file hash တွေကို map[[32]byte]string ထဲ သိမ်းပြီး တူညီတဲ့ file တွေကို ရှာတာ၊ grid game မှာ [2]int{x, y} ကို key လုပ်ပြီး သွားပြီးသား နေရာတွေ မှတ်တာမျိုးပါ။ Slice ကိုတော့ == နဲ့ နှိုင်းယှဉ်လို့ မရလို့ map key မလုပ်နိုင်ပါဘူး။ Struct ထဲမှာ array ထည့်ထားရင် struct copy တိုင်းမှာ array ပါ copy ကူးတာကိုလည်း မှတ်ထားပါ။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Loop မှာ <= len() သုံးမိတာ
for i := 0; i <= len(scores); i++ က နောက်ဆုံး အကြိမ်မှာ scores[3] ကို ဖတ်ပြီး "panic: runtime error: index out of range [3] with length 3" နဲ့ ရပ်သွားပါတယ်။ i < len(scores) ကို သုံးပါ၊ ဒါမှမဟုတ် for i := range scores လို range ကို သုံးရင် index ဘယ်တော့မှ မကျော်ပါဘူး။
2. Array ကို function ထဲမှာ ပြင်ပြီး caller မှာ ပြောင်းမယ်လို့ ထင်တာ
Problem section မှာ မြင်ခဲ့သလို function က copy ကိုပဲ ရပါတယ်။ ပြင်ပြီးသား array ကို return ပြန်ပြီး scores = resetFirst(scores) လို ပြန်သိမ်းပါ၊ ဒါမှမဟုတ် *[3]int pointer ပို့ပါ။ Data အရေအတွက် ပြောင်းနိုင်ရင်တော့ slice ကို သုံးတာ ပိုသဘာဝကျပါတယ်။
3. Length မတူတဲ့ array တွေကို type တူတယ်လို့ ထင်တာ
func total(a [5]int) int ကို [3]int နဲ့ ခေါ်လို့ မရပါဘူး၊ length တစ်ခုစီအတွက် function တစ်ခုစီ လိုလာပါတယ်။ Length အမျိုးမျိုး လက်ခံဖို့ လိုရင် parameter ကို []int slice အဖြစ် ပြောင်းပြီး arr[:] နဲ့ ပို့ပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Size က data ရဲ့ အဓိပ္ပာယ် ဖြစ်နေမှ (hash၊ color၊ coordinate) array ကို ရွေးပြီး ကျန်တဲ့ နေရာတွေမှာ slice ကို ဦးစားပေးပါ။
- Loop တွေမှာ 3 လို number ကို hard-code မလုပ်ဘဲ len(a) ဒါမှမဟုတ် for i := range a ကို သုံးပါ၊ length ပြောင်းရင် code က လိုက်ပြောင်းပါတယ်။
- Array ကြီးတွေကို function ဆီ ခဏခဏ ပို့ရရင် copy ကုန်ကျစရိတ်ကို သတိထားပြီး pointer ဒါမှမဟုတ် a[:] slice ကို ပို့ပါ။
- Function က array ကို ပြင်ရမယ် ဆိုရင် signature ကနေ ထင်ရှားအောင် ပြင်ပြီးသား array ကို return ပြန်ပါ။
Mini challenge
Mini challenge: တစ်ပတ်စာ အပူချိန်
[7]float64 array တစ်ခုထဲမှာ တစ်ပတ်စာ အပူချိန်တွေ သိမ်းပြီး ပျမ်းမျှ၊ အပူဆုံးနေ့ ရှာတဲ့ function တွေနဲ့ copy တစ်ခုကို ပြင်ပြီး ပြန်ပေးတဲ့ function တစ်ခု ရေးပါ။
- average([7]float64) float64 — ပျမ်းမျှ ပြန်ပေးရမယ်
- hottest([7]float64) (day string, temp float64) — [7]string နေ့နာမည် array ကို သုံးရမယ်
- clearWeekend([7]float64) [7]float64 — Sat နဲ့ Sun ကို 0 ပြောင်းထားတဲ့ copy ကို ပြန်ပေးရမယ်
- main() မှာ မူလ week array မပြောင်းကြောင်း print ထုတ်ပြရမယ်
package main
import "fmt"
var days = [7]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}
func average(temps [7]float64) float64 {
// TODO: add every value and divide by len(temps)
return 0
}
func hottest(temps [7]float64) (day string, temp float64) {
// TODO: remember the index of the highest value
return "", 0
}
func clearWeekend(temps [7]float64) [7]float64 {
// TODO: set index 5 and 6 to 0 and return the copy
return temps
}
func main() {
week := [7]float64{31.5, 33, 34.5, 32, 36, 35.5, 30}
// TODO: print the average with %.2f, the hottest day,
// then week and clearWeekend(week) on separate lines
fmt.Println(week)
}ရလဒ်: Run လိုက်ရင် ဒီလို ထွက်ရပါမယ်: average: 33.21 hottest: Fri 36 week: [31.5 33 34.5 32 36 35.5 30] weekdays: [31.5 33 34.5 32 36 0 0]
အရိပ်အမြွက် ကြည့်ရန်
hottest() မှာ best := 0 နဲ့ စပြီး for i := range temps ထဲမှာ temps[i] > temps[best] ဖြစ်ရင် best = i လို့ ပြောင်းပါ။ clearWeekend() ရဲ့ parameter က copy ဖြစ်ပြီးသားမို့ တိုက်ရိုက် ပြင်ပြီး return ပြန်လို့ ရပါတယ်။
အဖြေ ကြည့်ရန်
package main
import "fmt"
var days = [7]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}
func average(temps [7]float64) float64 {
total := 0.0
for _, t := range temps {
total += t
}
return total / float64(len(temps))
}
func hottest(temps [7]float64) (day string, temp float64) {
best := 0
for i := range temps {
if temps[i] > temps[best] {
best = i
}
}
return days[best], temps[best]
}
func clearWeekend(temps [7]float64) [7]float64 {
temps[5], temps[6] = 0, 0
return temps
}
func main() {
week := [7]float64{31.5, 33, 34.5, 32, 36, 35.5, 30}
fmt.Printf("average: %.2f\n", average(week))
day, temp := hottest(week)
fmt.Println("hottest:", day, temp)
weekdays := clearWeekend(week)
fmt.Println("week: ", week)
fmt.Println("weekdays:", weekdays)
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- [N]T မှာ N က constant ဖြစ်ပြီး type ရဲ့ အစိတ်အပိုင်းပါ၊ [3]int နဲ့ [4]int က type မတူပါဘူး။
- ကြေညာရုံပဲ ဆိုရင် element တိုင်း zero value ဖြစ်ပြီး [...] က length ကို compiler ကို ရေတွက်ခိုင်းပါတယ်။
- Array ကို assign လုပ်တာ၊ function ဆီ ပို့တာ၊ return ပြန်တာတိုင်းက element အားလုံးကို copy ကူးပါတယ်။
- Constant index မှားရင် compile error၊ runtime index မှားရင် panic ဖြစ်ပြီး comparable array တွေကို == နဲ့ နှိုင်းယှဉ်၊ map key လုပ်လို့ ရပါတယ်။
A Tour of Go — Go