ရိုးရိုးလေး ရှင်းပြမယ်
Data type ကို ရိုးရိုးလေး ပြောရရင်
Go က statically typed language ဖြစ်လို့ variable တိုင်း ဘယ်လို data သိမ်းမလဲ ဆိုတာ compiler က ကြိုသိထားရပါတယ်။ Type ဆိုတာ data ရဲ့ အမျိုးအစားပါ — ကိန်းပြည့်လား၊ ဒသမကိန်းလား၊ စာသားလား၊ မှန်/မှား လား။
ဈေးဆိုင်က ဘီဒိုမှာ အံဆွဲတွေ သီးသန့် ခွဲထားသလိုပါပဲ: ပိုက်ဆံ အံဆွဲထဲ ဖိနပ် မထည့်ပါဘူး။ Go ကလည်း int အံဆွဲထဲက value နဲ့ float64 အံဆွဲထဲက value ကို ကိုယ်တိုင် မရောပေးပါဘူး၊ ရောချင်ရင် 'ဒီ value ကို ဒီ type ပြောင်းပါ' လို့ သင်ကိုယ်တိုင် ရေးရပါတယ်။ ဒီ basic type တွေကို နားလည်ထားရင် function, struct, slice, map တွေ ရေးရတာ အများကြီး လွယ်သွားပါတယ်။
Problem → Why → Solution
ပြဿနာ: number နဲ့ string၊ int နဲ့ float ကို တိုက်ရိုက် ပေါင်းလို့ မရ
Order တစ်ခုရဲ့ စုစုပေါင်းကို တွက်ဖို့ အရေအတွက် `age` (int) ကို ဈေးနှုန်း `price` (float64) နဲ့ မြှောက်ပြီး label စာသားနဲ့ ဆက်ချင်ပါတယ်။ JavaScript မှာ ဒါမျိုး ဘာမှ မပြောဘဲ အလုပ်လုပ်ပေမယ့် Go compiler က line နှစ်ကြောင်းလုံးကို ငြင်းပါတယ်။ Program က စတောင် မ run ရသေးပါဘူး။
age := 25
price := 19.99
total := age * price // invalid operation: age * price
// (mismatched types int and float64)
label := "Age: " + age // invalid operation: "Age: " + age
// (mismatched types untyped string and int)ဘာကြောင့် Go က အလိုအလျောက် မပြောင်းပေးတာလဲ
Go မှာ implicit conversion လုံးဝ မရှိပါဘူး။ int ကို float64 အဖြစ် ပြောင်းတာ၊ float ကို int ပြောင်းရင် ဒသမ ပျောက်တာ၊ number ကို စာသား ပြောင်းတာ — ဒါတွေက အဓိပ္ပာယ် ကွဲနိုင်တဲ့ ဆုံးဖြတ်ချက်တွေမို့ compiler က ခန့်မှန်းမလုပ်ဘဲ သင့်ကို ရွေးခိုင်းပါတယ်။ `float64(age) * price` နဲ့ `"Age: " + strconv.Itoa(age)` လို့ ရေးလိုက်ရင် code ကို ဖတ်တဲ့သူတိုင်း ဘာဖြစ်နေလဲ ရှင်းရှင်းလင်းလင်း မြင်ရပါတယ်။
အဓိက သဘောတရား: basic type များနဲ့ conversion
| Type | သိမ်းတဲ့ data | Zero value |
|---|---|---|
| bool | true သို့ false | false |
| string | UTF-8 byte တွေ (ပြောင်းလို့ မရ) | "" |
| int, int8 ... int64 | အပေါင်း/အနုတ် ကိန်းပြည့် | 0 |
| uint, uint8 ... uint64 | အနုတ်မပါတဲ့ ကိန်းပြည့် | 0 |
| byte (= uint8) | Byte တစ်ခု | 0 |
| rune (= int32) | Unicode code point တစ်ခု | 0 |
| float32, float64 | ဒသမကိန်း | 0 |
Type မရေးဘဲ `:=` သုံးရင် literal ကို ကြည့်ပြီး default type ကို ရွေးပါတယ်: `25` က `int`၊ `19.99` က `float64`၊ `'T'` က `rune`၊ `"Go"` က `string` ဖြစ်ပါတယ်။ `int` ရဲ့ အရွယ်အစားက platform ပေါ် မူတည်ပြီး 64-bit စက်တွေမှာ 64 bits ပါ။ ပုံမှန် ကိန်းပြည့်အတွက် `int`၊ ဒသမကိန်းအတွက် `float64` ကို သုံးပြီး file format ဒါမှမဟုတ် network protocol က တောင်းမှ `int32`, `uint8` လို အရွယ်အစား တိကျတဲ့ type တွေကို သုံးပါ။
Type တစ်ခုကနေ တစ်ခုကို ပြောင်းဖို့ `T(v)` ပုံစံကို သုံးပါတယ်: `float64(age)`၊ `int(price)`။ Float variable ကို int ပြောင်းရင် ဒသမကို ဖြတ်ပစ်ပြီး သုည ဘက်ကို ယူလို့ `price` က 19.99 ဆိုရင် `int(price)` က `19` ဖြစ်ပါတယ်၊ round မလုပ်ပါဘူး (`int(19.99)` လို constant ကို တိုက်ရိုက် ပြောင်းရင်တော့ ဒသမ ပျောက်မှာမို့ compile error ပါ)။ Integer အချင်းချင်း စားရင်လည်း ရလဒ်က integer ဖြစ်လို့ `7 / 2` က `3` ပါ။
String နဲ့ number ကြား ပြောင်းဖို့ကတော့ `T(v)` မဟုတ်ဘဲ `strconv` package ကို သုံးရပါတယ်။ `strconv.Itoa(65)` က `"65"` ပြန်ပေးပြီး `strconv.Atoi("65")` က number နဲ့ error နှစ်ခု ပြန်ပေးပါတယ် — စာသားက ဂဏန်း မဟုတ်ရင် error ရလို့ပါ။ `string(65)` လို့ ရေးမိရင်တော့ `"65"` မဟုတ်ဘဲ code point 65 ရဲ့ စာလုံး `"A"` ရပါတယ်။
String တွေက immutable ဖြစ်ပါတယ်၊ ဖန်တီးပြီးရင် အထဲက byte တွေကို ပြင်လို့ မရပါဘူး။ `s[0] = 'G'` လို့ ရေးရင် `cannot assign to s[0] (neither addressable nor a map index expression)` compile error ရပါတယ်။ စာသားကို ပြောင်းချင်ရင် string အသစ် တည်ဆောက်ရပါတယ် — `strings.ToUpper` လို function သုံးတာ၊ ဒါမှမဟုတ် `[]byte(s)` ဒါမှမဟုတ် `[]rune(s)` အဖြစ် ပြောင်းပြီး ပြင်ကာ `string(...)` နဲ့ ပြန်ပြောင်းတာမျိုးပါ။ String နှစ်ခုကို `==` နဲ့ နှိုင်းယှဉ်ရင် အထဲက content ကို byte တစ်ခုချင်း နှိုင်းယှဉ်ပါတယ်။
- Value နှစ်ခုရဲ့ type ကို စစ်
- Type တူရင် တိုက်ရိုက် တွက်
- Number ချင်းဆို T(v) နဲ့ ပြောင်း
- String ↔ number ဆို strconv သုံး
Go က type ကို ဘယ်တော့မှ ကိုယ်တိုင် မပြောင်းပေးပါဘူး၊ ပြောင်းတဲ့ နေရာတိုင်းက code ထဲမှာ မြင်ရပါတယ်။
Type ကြေညာခြင်းနဲ့ conversion ပုံစံ
var name Type = value // explicit type
inferred := literal // default type from the literal
converted := TargetType(value) // number <-> number
text := strconv.Itoa(number) // int -> string
number, err := strconv.Atoi(text) // string -> int (may fail)
const untyped = 10 // adapts to the context
const typed Type = 10 // always TypeNumber type အချင်းချင်း ပြောင်းဖို့ `TargetType(value)`၊ string နဲ့ int ကြား ပြောင်းဖို့ `strconv` ကို သုံးပါတယ်။ `Atoi` က error ပါ ပြန်ပေးလို့ အမြဲ စစ်ရပါမယ်။ Constant ကို type မပေးရင် untyped ဖြစ်ပြီး သုံးတဲ့ နေရာနဲ့ လိုက်ပြောင်းပါတယ်။
Example: %T နဲ့ type တွေကို ကြည့်ပြီး ပြောင်းကြည့်ရအောင်
`%T` verb က value ရဲ့ type ကို print ထုတ်ပေးပါတယ်။ ဒီ program က variable ခြောက်ခုရဲ့ type ကို ပြပြီး int နဲ့ float64 ကို ပေါင်းတွက်ပါတယ်။ `initial` (rune) အတွက် `%T` က ဘာပြမလဲ ကြိုမှန်းကြည့်ပါ — `rune` လို့ ထင်ရင် သင်ယူစရာ တစ်ခု တွေ့ပါပြီ။
package main
import "fmt"
func main() {
age := 25
price := 19.99
name := "ThutaTech"
active := true
initial := 'T'
var level uint8 = 255
fmt.Printf("age: %T\n", age)
fmt.Printf("price: %T\n", price)
fmt.Printf("name: %T\n", name)
fmt.Printf("active: %T\n", active)
fmt.Printf("initial: %T %v %c\n", initial, initial, initial)
fmt.Printf("level: %T %d\n", level, level)
total := float64(age) * price
fmt.Printf("total: %.2f\n", total)
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Default types
price := 19.99ဒသမပါတဲ့ literal ဖြစ်လို့ default type `float64` ကို ရပါတယ်။ `age := 25` ကတော့ `int` ဖြစ်ပါတယ်။
- A rune is an int32
fmt.Printf("initial: %T %v %c\n", initial, initial, initial)`rune` က `int32` ရဲ့ alias (နာမည်ကွဲ) ဖြစ်လို့ `%T` က `int32` လို့ ပြပါတယ်။ `%v` က number `84` (T ရဲ့ code point) ကို ပြပြီး `%c` က စာလုံး `T` ကို ပြပါတယ်။
- A sized type
var level uint8 = 255`uint8` က 0 ကနေ 255 အထိပဲ သိမ်းနိုင်ပါတယ်။ `256` ပေးရင် constant overflow ဖြစ်လို့ compile error ရပါမယ်။ `byte` ဆိုတာလည်း ဒီ `uint8` ပါပဲ။
- Explicit conversion
total := float64(age) * price`age` ကို `float64` ပြောင်းပြီးမှ `price` နဲ့ မြှောက်လို့ type နှစ်ခု ကိုက်ပါတယ်။ Conversion မပါရင် `mismatched types int and float64` error ဖြစ်ပါမယ်။
- Rounding only for display
fmt.Printf("total: %.2f\n", total)`%.2f` က ဒသမ နှစ်နေရာနဲ့ ပြရုံပဲ ဖြစ်ပြီး `total` ထဲက value ကို မပြောင်းပါဘူး။ Float64 ရဲ့ binary ပုံစံကြောင့် တကယ့် value က 499.75 အတိအကျ မဟုတ်နိုင်ပေမယ့် ပြတဲ့အခါ `499.75` ထွက်ပါတယ်။
age: int
price: float64
name: string
active: bool
initial: int32 84 T
level: uint8 255
total: 499.75String, byte နဲ့ rune
Go string က UTF-8 byte တွေရဲ့ အစဉ်ပါ။ `len(s)` က byte အရေအတွက်ကို ပြန်ပေးပြီး စာလုံးအရေအတွက် မဟုတ်ပါဘူး။ English စာလုံးတွေက တစ်လုံး 1 byte ဖြစ်လို့ ကွာခြားချက် မမြင်ရပေမယ့် Myanmar စာလုံး (code point) တစ်ခုက UTF-8 မှာ 3 bytes ယူပါတယ်။ `s[0]` က byte (`uint8`) ကို ပြန်ပေးပြီး `for range` နဲ့ string ကို loop ပတ်ရင်တော့ rune တစ်ခုချင်းကို ရပါတယ်။ Code point အရေအတွက်ကို `utf8.RuneCountInString` နဲ့ ရေတွက်ပါ။
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
word := "Go"
greeting := "မင်္ဂလာပါ"
fmt.Println(len(word), utf8.RuneCountInString(word))
fmt.Println(len(greeting), utf8.RuneCountInString(greeting))
fmt.Println(word[0], string(word[0]))
var small int8 = 126
for range 3 { // range over an int: Go 1.22 and later
small++
fmt.Println(small)
}
}ဒီ program က `2 2`၊ `27 9`၊ `71 G` ကို print ထုတ်ပြီးမှ `127`, `-128`, `-127` ကို ထုတ်ပါတယ်။ "မင်္ဂလာပါ" မှာ code point ၉ ခု ရှိပြီး byte ၂၇ ခု ယူပါတယ်။ နောက်ဆုံးအပိုင်းကတော့ integer overflow ပါ: `int8` ရဲ့ အများဆုံး value 127 ကို ကျော်တဲ့အခါ run time မှာ error မတက်ဘဲ -128 ကို ပတ်လည် ပြန်သွားပါတယ်။ `for range 3` က Go 1.22 နဲ့ နောက်ပိုင်းမှာ integer ပေါ် range လုပ်ပြီး သုံးကြိမ် ထပ်လုပ်တဲ့ ပုံစံပါ။
Typed နဲ့ untyped constant
`const limit = 10` လို type မပေးထားတဲ့ constant က untyped ဖြစ်ပါတယ်။ သုံးတဲ့ နေရာမှာ လိုတဲ့ type ကို လိုက်ယူလို့ `float64` နဲ့ မြှောက်လည်း ရ၊ `int` နဲ့ ပေါင်းလည်း ရပါတယ်။ `const maxUsers int = 10` ကတော့ typed constant ဖြစ်လို့ variable တစ်ခုလိုပဲ `int` အဖြစ်ပဲ ရှိပြီး `float64` နဲ့ ရောရင် conversion လိုပါတယ်။ Untyped constant တွေကို compiler က တိကျမှု မြင့်မြင့်နဲ့ တွက်လို့ `1 << 40` လို ကြီးတဲ့ value တွေကိုလည်း ကြားအဆင့်မှာ သုံးလို့ ရပါတယ်။
package main
import "fmt"
const limit = 10 // untyped: adapts to where it is used
const maxUsers int = 10 // typed: always an int
func main() {
ratio := 2.5
fmt.Println(ratio * limit) // limit acts as a float64
fmt.Println(float64(maxUsers) * ratio) // typed: convert first
fmt.Println((1 << 40) / (1 << 30)) // exact constant arithmetic
}Output က `25`, `25`, `1024` ပါ။ ဒုတိယ line မှာ `float64(...)` ကို ဖြုတ်လိုက်ရင် `mismatched types float64 and int` compile error ရပါမယ်။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ type ရွေးချယ်မှု
ငွေကြေး တွက်ချက်မှုမှာ float64 ကို ရှောင်ကြပါတယ်။ 0.1 နဲ့ 0.2 ကို သိမ်းထားတဲ့ float64 variable နှစ်ခုကို ရိုးရိုး ပေါင်းတာကတောင် binary floating point ကြောင့် `0.30000000000000004` ထွက်လို့ production system တွေက ငွေပမာဏကို အသေးဆုံး ယူနစ် (cent, pya) နဲ့ `int64` အဖြစ် သိမ်းကြပါတယ်၊ ဒါမှမဟုတ် decimal library တစ်ခုကို သုံးကြပါတယ်။ Database ID တွေ၊ file size တွေအတွက် `int64`၊ binary data နဲ့ network buffer တွေအတွက် `[]byte` ကို အသုံးများပါတယ်။
User ရိုက်ထည့်တဲ့ data နဲ့ URL query parameter တွေက အမြဲ string အဖြစ် ရောက်လာလို့ backend code မှာ `strconv.Atoi` နဲ့ `strconv.ParseFloat` ကို နေ့တိုင်းလို သုံးရပါတယ်။ Error ကို မစစ်ဘဲ ပစ်ထားရင် "abc" လို input က သုည ဖြစ်သွားပြီး မှားတဲ့ order တွေ ဖြစ်လာနိုင်ပါတယ်။ Myanmar စာသားကို ကိုင်တွယ်တဲ့ app တွေမှာ username အရှည် ကန့်သတ်ချက်လို နေရာမျိုးမှာ `len` အစား rune count ကို သုံးမှ user မြင်တဲ့ စာလုံးအရေအတွက်နဲ့ ပိုနီးစပ်ပါတယ်။
အမှားများ
Data type တွေမှာ အဖြစ်များတဲ့ အမှားတွေ
1. Number ကို string(n) နဲ့ စာသား ပြောင်းတာ
`string(65)` က `"65"` မဟုတ်ဘဲ `"A"` ပါ၊ integer ကို code point အဖြစ် ယူလို့ပါ။ `go vet` ကလည်း 'conversion from int to string yields a string of one rune' ဆိုပြီး သတိပေးပါတယ်။ ဂဏန်းကို စာသားအဖြစ် လိုချင်ရင် `strconv.Itoa(n)` ဒါမှမဟုတ် `fmt.Sprint(n)` ကို သုံးပါ။
2. len() ကို စာလုံးအရေအတွက်လို့ ထင်တာ
`len("မင်္ဂလာပါ")` က 9 မဟုတ်ဘဲ 27 ပါ၊ byte တွေကို ရေတွက်လို့ပါ။ Myanmar စာသားကို `s[:10]` လို byte index နဲ့ ဖြတ်ရင် စာလုံးတစ်ဝက်မှာ ပြတ်ပြီး မမှန်တဲ့ UTF-8 ဖြစ်သွားနိုင်ပါတယ်။ Code point ရေတွက်ဖို့ `utf8.RuneCountInString` ကို၊ ဖြတ်ဖို့ `[]rune(s)` ကို သုံးပါ။
3. Atoi ရဲ့ error ကို `_` နဲ့ ပစ်တာ
`qty, _ := strconv.Atoi(input)` လို့ ရေးရင် input မှားတဲ့အခါ `qty` က 0 ဖြစ်ပြီး program က ဘာမှ မပြောဘဲ ဆက် run ပါတယ်။ `if err != nil` နဲ့ အမြဲ စစ်ပြီး user ကို ပြန်ပြောပါ။
4. ငွေကို float64 နဲ့ သိမ်းတာ
Floating point က ဒသမ တချို့ကို အတိအကျ မသိမ်းနိုင်လို့ ပေါင်းရင်း နုတ်ရင်း cent အပိုင်းအစတွေ ကွဲလာပါတယ်။ ငွေပမာဏကို integer cent အဖြစ် သိမ်းပြီး ပြတဲ့အခါမှ `%d.%02d` နဲ့ ပုံစံချပါ။
Mini challenge
Mini challenge: type မှန်တဲ့ ပြေစာ
လက်ဖက်ရည် ဈေးနှုန်းကို cent နဲ့ သိမ်းပြီး စာသားအဖြစ် ရောက်လာတဲ့ အရေအတွက်ကို number ပြောင်းကာ ပြေစာ တစ်ခု print ထုတ်ပါ။
- `item := "လက်ဖက်ရည်"`၊ `qtyText := "3"`၊ `priceCents := 125` ကို သုံးရမယ်
- `strconv.Atoi` နဲ့ အရေအတွက် ပြောင်းပြီး error ကို `if err != nil` နဲ့ စစ်ရမယ်
- Item နာမည်ရဲ့ rune အရေအတွက်နဲ့ byte အရေအတွက် နှစ်ခုလုံးကို print ထုတ်ရမယ်
- စုစုပေါင်းကို `$3.75` ပုံစံနဲ့ ပြပြီး `strconv.Itoa` နဲ့ cent စာသား line တစ်ကြောင်းလည်း ထုတ်ရမယ်
package main
import "fmt" // TODO: group this with strconv and unicode/utf8
func main() {
item := "လက်ဖက်ရည်"
qtyText := "3"
priceCents := 125
// TODO: qty, err := ... convert qtyText; on error print it and return
// TODO: totalCents := ...
// TODO: print the item, its rune count and its byte count
// TODO: print "total: $D.CC" using / and % on totalCents
// TODO: print "as text: <cents> cents" using strconv.Itoa and +
}ရလဒ်: item: လက်ဖက်ရည် runes: 9 bytes: 27 total: $3.75 as text: 375 cents
အရိပ်အမြွက် ကြည့်ရန်
`totalCents/100` က ဒေါ်လာ၊ `totalCents%100` က cent ဖြစ်ပါတယ်။ `%02d` က cent ကို ဂဏန်းနှစ်လုံး ဖြစ်အောင် ရှေ့မှာ 0 ဖြည့်ပေးလို့ 5 cent ဆိုရင် `05` ပြပါတယ်။ `strconv` နဲ့ `unicode/utf8` ကို import ထဲ ထည့်ရမယ်ဆိုတာနဲ့ ထည့်ထားတဲ့ package တိုင်းကို သုံးရမယ်ဆိုတာ မမေ့ပါနဲ့။
အဖြေ ကြည့်ရန်
package main
import (
"fmt"
"strconv"
"unicode/utf8"
)
func main() {
item := "လက်ဖက်ရည်"
qtyText := "3"
priceCents := 125
qty, err := strconv.Atoi(qtyText)
if err != nil {
fmt.Println("bad quantity:", err)
return
}
totalCents := qty * priceCents
runes := utf8.RuneCountInString(item)
fmt.Println("item:", item, "runes:", runes, "bytes:", len(item))
fmt.Printf("total: $%d.%02d\n", totalCents/100, totalCents%100)
fmt.Println("as text: " + strconv.Itoa(totalCents) + " cents")
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Literal တွေရဲ့ default type က int, float64, rune (= int32), string ဖြစ်ပြီး byte က uint8 ရဲ့ နာမည်ကွဲပါ။
- Go မှာ implicit conversion မရှိလို့ number type ချင်း T(v) နဲ့ ကိုယ်တိုင် ပြောင်းရပါတယ်၊ float → int က ဒသမကို ဖြတ်ပါတယ်။
- String နဲ့ number ကြားမှာ strconv ကို သုံးပါ၊ string(n) က စာလုံး တစ်လုံးကို ပေးပါတယ်။
- len(s) က byte ကို ရေတွက်ပြီး Myanmar စာလုံး ရေတွက်ဖို့ utf8.RuneCountInString ကို သုံးပါ။
- Untyped constant က သုံးတဲ့နေရာနဲ့ လိုက်ပြောင်းပြီး typed constant က သူ့ type ပဲ ရှိပါတယ်။
A Tour of Go — Go