Thuta Learning
ရှာဖွေရန်
IntermediateProgrammingbeginner

Arrays

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

Array သည် size fixed ဖြစ်တဲ့ collection ပါ။ တူညီတဲ့ type ရှိတဲ့ value တွေကို index နဲ့သိမ်းပါတယ်။ Go မှာ array size က type ရဲ့အစိတ်အပိုင်းဖြစ်လို့ [3]int နဲ့ [4]int က မတူတဲ့ type တွေပါ။

go
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))
}

scores[0] က ပထမဆုံး item ကိုယူတာပါ။ Programming မှာ index အများစုက 0 ကစတာကြောင့် ပထမ item သည် index 0 ဖြစ်ပါတယ်။

You should see
[80 90 100] first score: 80 total items: 3

Info

Array က fixed size ဖြစ်လို့ data အရေအတွက်ပြောင်းနိုင်တဲ့ real project တွေမှာ slice ကိုပိုသုံးကြပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • scores[3] လို index range ကျော်ပြီးယူရင် error ဖြစ်ပါတယ်။ အမြဲ len() နဲ့ size ကိုသိထားပါ။
Arrays | Thuta Learning