Slices

Slices #

// _Slices_ data type က Go မှာတော်တော်အသုံးဝင်တယ်။
// array ထက်တောင်ပိုပီးအသုံးဝင်တယ်။

package main

import "fmt"

func main() {

	// arrays နဲ့မတူတာက slices မှာ elements တွေရဲ့ type တွေဘဲသတ်မှတ်ပေးစရာလိုတယ်။
	// ဘယ်လောက် size ဘာညာသတ်မှတ်စရာမလိုဘူး။
	// အစကထဲကဘယ်လောက် size ရှိမယ်ဘာညာသိစရာမလိုဘူး။
	// ဒီ code မှာဆိုရင် buildin `make` ကိုသုံးပီးတော့ length 3 ခုပါတဲ့ slice တခုဖန်တီးလိုက်တယ်။
	// (default အနေနဲ့ zero value သတ်မှတ်သွားပါလိမ့်မယ်)
	s := make([]string, 3)
	fmt.Println("emp:", s)

	// set/get တွေကတော့ array မှာလိုပါဘဲ
	s[0] = "a"
	s[1] = "b"
	s[2] = "c"
	fmt.Println("set:", s)
	fmt.Println("get:", s[2])

	// `len` ကတော့ သိတဲ့အတိုင်း slice ရဲ့ length ကိုပြန်ပေးပါလိမ့်မယ်။
	fmt.Println("len:", len(s))

	// slices က array ထက်ပိုအသုံးဝင်တဲ့ baisc operations တွေပါတယ်။
	// နမူနာပြောရမယ်ဆိုရင် `append` ပေါ့။ `append` မှာတခုသတိထားရမှာက
	// return value ကို accept လုပ်ရမယ်၊ ဘာလို့လဲဆိုရင်တော့ slice value အသစ်ပြန်ရချင်ရမှာမလို့ပါ။
	s = append(s, "d")
	s = append(s, "e", "f")
	fmt.Println("apd:", s)

	// slice တခုကနေပီးတော့ နောက်တခုကို copy ကူးလို့ရပါတယ်။
	// အခုဒီကုဒ်မှာဆိုရင် `make` ကိုသုံးပြီးတော့ s နဲ့ len တူတဲ့ slice တခုကိုတည်ဆောက်လိုက်ပါတယ်
	//ပီးတော့ `s` ကနေပီးတော့ `c` ထဲကို copy ကူးလိုက်တာပါ။
	c := make([]string, len(s))
	copy(c, s)
	fmt.Println("cpy:", c)

	// Slices မှာ "slice" operator ကိုဒီလို `slice[low:high]` syntax နှင့်သုံးပီးတော့
	// ကိုယ်လိုချင်တဲ့အပိုင်းကိုဘဲဖြတ်ပီးသုံးနိုင်ပါတယ်။ ဒီဥပမာမှာဆိုရင်ကျနော်တို့က s slice ရဲ့အစ
	// `s[2]` ကနေပီးတော့ `s[4]` ထိ item တွေကိုဘဲဖြတ်ပီးသုံးနိုင်ပါတယ်။
	l := s[2:5]
	fmt.Println("sl1:", l)

	// ဒီဥမာကတော့ အစကနေပီးတော့ `s[5]` မတိုင်ခင်ထိ ဖြတ်ပီးသုံးပြထားတာပါ။
	l = s[:5]
	fmt.Println("sl2:", l)

	// ဒီဥပမာကတော့ `s[2]` ကနေနောက်ပိုင်းအကုန်လုံးကိုဘဲဖြတ်ပီးသုံးပြထားတာပါ။
	l = s[2:]
	fmt.Println("sl3:", l)

	// slice ကို line တခုထဲနှင့် initialize လုပ်ပီးတော့လဲအသုံးပြုနိုင်တယ်။
	t := []string{"g", "h", "i"}
	fmt.Println("dcl:", t)

	// Slices တွေကို slice ထဲထည့်ပီး multi-dimensional data structure အဖြစ်
	// အသုံးပြုနိုင်တယ်။ ပုံမှန် multi-dimensional array တွေနဲ့မတူတာကသူက
	// fixed length ဖြစ်မနေဘဲ အမျိုးမျိုးပြောင်းလဲနေနိုင်ပါတယ်။
	twoD := make([][]int, 3)
	for i := 0; i < 3; i++ {
		innerLen := i + 1
		twoD[i] = make([]int, innerLen)
		for j := 0; j < innerLen; j++ {
			twoD[i][j] = i + j
		}
	}
	fmt.Println("2d: ", twoD)
}
# Note that while slices are different types than arrays,
# they are rendered similarly by `fmt.Println`.
$ go run slices.go
emp: [  ]
set: [a b c]
get: c
len: 3
apd: [a b c d e f]
cpy: [a b c d e f]
sl1: [c d e]
sl2: [a b c d e]
sl3: [c d e f]
dcl: [g h i]
2d:  [[0] [1 2] [2 3 4]]

# Check out this [great blog post](https://go.dev/blog/slices-intro)
# by the Go team for more details on the design and
# implementation of slices in Go.

# Now that we've seen arrays and slices we'll look at
# Go's other key builtin data structure: maps.