ရိုးရိုးလေး ရှင်းပြမယ်
Go ကို ရိုးရိုးလေး နားလည်မယ်
Go ဆိုတာ Google မှာ စတင် ဖန်တီးခဲ့တဲ့ compiled programming language တစ်ခုပါ။ 'Compiled' ဆိုတာ သင်ရေးတဲ့ code ကို run မတိုင်ခင် compiler က ကွန်ပျူတာ နားလည်တဲ့ machine code အဖြစ် ဘာသာပြန်ထားတာကို ဆိုလိုပါတယ်။ ဟင်းချက်နည်းကို ချက်ပြီးသား ဟင်းအဖြစ် ထုပ်ပိုးပေးလိုက်တာနဲ့ တူပါတယ် — စားမယ့်သူက ချက်နည်း မသိလည်း ရပါတယ်။
Go ရဲ့ ရည်မှန်းချက်က ရိုးရှင်းမှု၊ မြန်ဆန်မှုနဲ့ အလုပ်တွေကို တစ်ပြိုင်နက် လုပ်နိုင်မှုပါ။ ဒါကြောင့် server-side application တွေမှာ မြန်ပြီး တည်ငြိမ်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: ကိုယ့်စက်မှာ run ပေမယ့် server မှာ မ run
Python လို interpreted language နဲ့ report script တစ်ခု ရေးပြီး ကိုယ့် laptop မှာ စမ်းကြည့်တော့ အဆင်ပြေပါတယ်။ ဒါပေမယ့် server ပေါ် copy ကူးပြီး run လိုက်တော့ package မရှိလို့ ချက်ချင်း ပျက်သွားပါတယ်။ Server မှာ interpreter version မှန်ဖို့၊ library တွေ install လုပ်ထားဖို့ လိုနေတာပါ။
Server ဆယ်လုံး၊ container အများကြီးနဲ့ ဆိုရင် ဒီပြဿနာက ပိုကြီးလာပါတယ်။ စက်တိုင်းမှာ runtime နဲ့ dependency တွေကို တစ်ပုံစံတည်း ဖြစ်အောင် ထိန်းရတာ အလုပ်ရှုပ်ပါတယ်။
# On your laptop
$ python3 report.py
Report ready
# On the server
$ python3 report.py
ModuleNotFoundError: No module named 'requests'Go က ဒီပြဿနာကို ဘယ်လို ဖြေရှင်းလဲ
Interpreted program က run တဲ့ စက်တိုင်းမှာ interpreter နဲ့ library တွေ ရှိနေဖို့ လိုပါတယ်။ Go မှာတော့ `go build` က သင့် code၊ သုံးထားတဲ့ package တွေနဲ့ Go runtime ကိုပါ executable file တစ်ခုတည်းထဲ ထည့်ပေးပါတယ်။ အဲဒီ file ကို OS နဲ့ CPU အမျိုးအစား တူတဲ့ server ဆီ copy ကူးပြီး run ရုံပါပဲ၊ server မှာ Go install လုပ်ထားစရာ မလိုပါဘူး။
အဓိက သဘောတရား: Go ရဲ့ design ရွေးချယ်မှုများ
Go ကို Google မှာ code base ကြီးကြီးတွေ build လုပ်ရတာ နှေးတာ၊ language တွေ ရှုပ်ထွေးလာတာ၊ multicore CPU တွေကို အပြည့်အဝ မသုံးနိုင်တာ စတဲ့ ပြဿနာတွေကို ဖြေရှင်းဖို့ design လုပ်ခဲ့တာပါ။ ဒါကြောင့် Go ရဲ့ feature တိုင်းနောက်မှာ ရည်ရွယ်ချက် တစ်ခုစီ ရှိပါတယ်။
Compile မြန်ပြီး binary တစ်ခုတည်း
`go run hello.go` က file ကို ယာယီ executable အဖြစ် compile လုပ်ပြီး ချက်ချင်း run ပါတယ်။ `go build` ကတော့ executable ကို folder ထဲမှာ သိမ်းထားပေးလို့ နောက်မှ ထပ်ခါထပ်ခါ run နိုင်ပါတယ်။
Static typing
Variable တိုင်းမှာ type ရှိပြီး compiler က type မကိုက်တာကို run မတိုင်ခင် ဖမ်းပါတယ်။ ဒါပေမယ့် `:=` ကြောင့် type ကို ခဏခဏ ကိုယ်တိုင် ရေးစရာ မလိုပါဘူး။
တင်းကြပ်တဲ့ compiler
Import လုပ်ပြီး မသုံးတဲ့ package နဲ့ ကြေညာပြီး မသုံးတဲ့ local variable တွေက warning မဟုတ်ဘဲ compile error ပါ။ စစချင်း တင်းကြပ်လွန်းသလို ခံစားရပေမယ့် project ကြီးလာတဲ့အခါ code သန့်ရှင်းနေစေပြီး build ကိုလည်း မြန်စေပါတယ်။
Garbage collection နဲ့ goroutine
Memory ကို runtime က အလိုအလျောက် ပြန်ရှင်းပေးပြီး `go` keyword တစ်ခုနဲ့ function ကို goroutine အဖြစ် တစ်ပြိုင်နက် run နိုင်ပါတယ်။
From source to a file you can ship
----------------------------------
hello.go --go build--> hello (hello.exe on Windows) --copy--> server
source compile + one file: your code, runs with
type check packages and the Go runtime no Go installedGo မှာ ရည်ရွယ်ချက်ရှိရှိ မပါတဲ့ feature တွေလည်း ရှိပါတယ်။ Class နဲ့ inheritance မရှိဘဲ struct နဲ့ method ကို သုံးပါတယ်။ Exception မရှိဘဲ function တွေက error ကို value တစ်ခုအဖြစ် return ပြန်ပါတယ်။ Language ငယ်လို့ keyword ၂၅ လုံးပဲ ရှိပြီး spec တစ်ခုလုံးကို အချိန်တိုအတွင်း ဖတ်လို့ ရပါတယ်။
Language ငယ်ပေမယ့် standard library ကတော့ ကြီးပါတယ်။ HTTP server ရေးဖို့ `net/http`၊ JSON ဖတ်ရေးဖို့ `encoding/json`၊ စာသား ကိုင်တွယ်ဖို့ `strings`၊ test ရေးဖို့ `testing` စတဲ့ package တွေ Go နဲ့အတူ ပါလာပြီးသားပါ။ ဒါကြောင့် ရိုးရိုး service တစ်ခုကို third-party library မသုံးဘဲ ရေးနိုင်ပါတယ်။ Tool ဘက်မှာလည်း `go` command တစ်ခုတည်းက build, run, test, format (`go fmt`), သံသယဖြစ်စရာ code စစ်တာ (`go vet`) နဲ့ dependency စီမံတာ (`go mod`) အကုန် လုပ်ပေးပါတယ်။
- .go file ရေးတယ်
- Compiler က type နဲ့ import ကို စစ်တယ်
- Executable တစ်ခု ထွက်လာတယ်
- OS က အဲဒါကို တိုက်ရိုက် run တယ်
Interpreter မလိုလို့ executable ကို run တဲ့ စက်မှာ Go install လုပ်ထားစရာ မလိုပါဘူး။
Package တစ်ခုထက်ပိုပြီး import လုပ်တဲ့ ပုံစံ
package main
import (
"packageone"
"packagetwo"
)
func main() {
packageone.Function(argument)
}Package တစ်ခုထက် ပိုသုံးရင် import တွေကို ကွင်း `( )` ထဲမှာ တစ်ကြောင်းတစ်ခု စုရေးပါတယ်။ Import လုပ်ထားတဲ့ package တိုင်းကို code ထဲမှာ အနည်းဆုံး တစ်ကြိမ် သုံးရပါမယ်၊ မဟုတ်ရင် compile error ဖြစ်ပါတယ်။ Package ထဲက function ကို `packagename.FunctionName` ပုံစံနဲ့ ခေါ်ပါတယ်။
Hello, Go! ကို အပိုင်းလိုက် ဖတ်ကြည့်ရအောင်
ဒီ program က စာတစ်ကြောင်းပဲ print ထုတ်ပေမယ့် Go program တိုင်းမှာ ပါရမယ့် အပိုင်းတွေ အကုန် ပါပါတယ်။ `hello.go` အဖြစ် save လုပ်ပြီး `go run hello.go` နဲ့ run ကြည့်ပါ။ ပြီးရင် `go build hello.go` နဲ့ executable ထုတ်ပြီး အဲဒီ file ကို တိုက်ရိုက် run ကြည့်ပါ။
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Package clause
package mainFile ထဲက code တွေက ဘယ် package ထဲ ပါလဲ ပြောတာပါ။ `main` ဆိုတဲ့ နာမည်က အထူးပါ — compiler က ဒီ package ကို executable program အဖြစ် build လုပ်ပါတယ်။
- Import
import "fmt"Standard library ထဲက `fmt` (format) package ကို ခေါ်ထားပါတယ်။ Screen ပေါ် စာထုတ်တဲ့ function တွေ ဒီ package ထဲမှာ ရှိပါတယ်။ ဒီ line ကို ထားပြီး `fmt` ကို မသုံးရင် `"fmt" imported and not used` error ရပါမယ်။
- Entry point
func main() {Program စတဲ့ နေရာပါ။ `main` function မှာ parameter မရှိ၊ return value မရှိပါဘူး။ ဒီ function ပြီးသွားတာနဲ့ program တစ်ခုလုံး ရပ်သွားပါတယ်။
- Calling an exported function
fmt.Println("Hello, Go!")`fmt` package ထဲက `Println` ကို ခေါ်ပြီး စာနဲ့ newline ကို print ထုတ်ပါတယ်။ `Println` က စာလုံးအကြီး P နဲ့ စလို့ package အပြင်ကနေ သုံးခွင့်ရှိတဲ့ exported name ဖြစ်ပါတယ်။
Hello, Go!Go ကို တခြား language တွေနဲ့ နှိုင်းယှဉ်ကြည့်ရင်
Go က language တိုင်းထက် ပိုကောင်းတယ်လို့ မဆိုလိုပါဘူး။ ဒါပေမယ့် program တစ်ခုကို ဘယ်လို run ပြီး ဘယ်လို ပို့ဆောင်လဲ ဆိုတာမှာ ကွာခြားချက်တွေ ရှင်းရှင်းလင်းလင်း ရှိပါတယ်။
| Language | Run ပုံ | Server မှာ လိုတာ |
|---|---|---|
| Go | Machine code အဖြစ် compile | Executable file တစ်ခုတည်း |
| Python | Interpreter က run | Python interpreter နဲ့ package များ |
| Java | Bytecode အဖြစ် compile ပြီး JVM က run | JVM (Java runtime) |
| C | Machine code အဖြစ် compile | Executable (memory ကို ကိုယ်တိုင် စီမံ) |
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ ဒီ design က ဘာအကျိုးရှိလဲ
Executable တစ်ခုတည်း ထွက်တာက container image တွေကို သေးသေးလေး ဖြစ်စေပါတယ်။ Go service တော်တော်များများကို runtime မပါတဲ့ base image ပေါ်မှာ binary တစ်ခုတည်း ထည့်ပြီး ship လုပ်နိုင်ပါတယ်။ Command-line tool တွေအတွက်လည်း user က download လုပ်ပြီး run ရုံပဲ ဖြစ်လို့ install လုပ်ရ လွယ်ပါတယ်။ `GOOS` နဲ့ `GOARCH` environment variable တွေ သတ်မှတ်ပြီး Windows ပေါ်ကနေ Linux server အတွက် build လုပ်တာမျိုး cross-compile လုပ်ရတာလည်း လွယ်ပါတယ်။
တင်းကြပ်တဲ့ compiler ကလည်း team အလုပ်မှာ အကျိုးရှိပါတယ်။ Code review လုပ်တဲ့အခါ မသုံးတော့တဲ့ import တွေ၊ မေ့ကျန်ခဲ့တဲ့ variable တွေကို လိုက်ရှာစရာ မလိုပါဘူး၊ compiler က အရင် ငြင်းပြီးသားပါ။ Code ပုံစံကိုလည်း `gofmt` က တစ်ပုံစံတည်း ဖြစ်အောင် ပြင်ပေးလို့ project ဘယ်လောက်ကြီးကြီး ဖတ်ရ လွယ်နေပါတယ်။ Compile မြန်တာကလည်း code ပြင်ပြီး test လုပ်တဲ့ loop ကို တိုစေလို့ commit တိုင်းမှာ test suite run တဲ့ project တွေအတွက် အရေးကြီးပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. File ကို .go extension နဲ့ မသိမ်းတာ
Notepad လို editor က `hello.go.txt` လို့ သိမ်းသွားတတ်ပါတယ်။ `go run` က `.go` နဲ့ ဆုံးတဲ့ file ကိုပဲ Go source အဖြစ် သိလို့ တခြား extension ဆိုရင် file ကို package နာမည်လို့ ထင်ပြီး error ပြပါတယ်။ File explorer မှာ extension တွေကို ဖော်ပြထားပြီး နာမည်ကို စစ်ပါ။
2. Import လုပ်ပြီး မသုံးတာ
Debug လုပ်ဖို့ `"os"` ကို import လုပ်ပြီး နောက်မှ သုံးတဲ့ code ကို ဖျက်လိုက်ရင် `"os" imported and not used` error တက်ပါတယ်။ Import line ကိုပါ ဖျက်ပါ၊ ဒါမှမဟုတ် editor ရဲ့ Go extension (`gopls`) ကို save လုပ်တိုင်း import တွေ အလိုအလျောက် ပြင်ခိုင်းပါ။
3. Class နဲ့ try/catch ကို ရှာနေတာ
Java ဒါမှမဟုတ် Python ကနေ လာတဲ့သူတွေက `class` keyword နဲ့ `try`/`catch` ကို ရှာတတ်ပါတယ်။ Go မှာ အဲဒါတွေ မရှိပါဘူး။ Data ကို struct နဲ့၊ behavior ကို method နဲ့ ရေးပြီး error ကို `if err != nil` နဲ့ စစ်ပါတယ်။ အဲဒါတွေကို Structs, Methods, Error Handling lesson တွေမှာ သင်ရပါမယ်။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Program အသစ်တိုင်းကို `go run` နဲ့ အရင် စမ်းပြီး ပို့ဆောင်ရမယ့်အခါမှ `go build` နဲ့ executable ထုတ်ပါ။
- Editor မှာ Go extension ထည့်ပြီး save လုပ်တိုင်း `gofmt` နဲ့ import ပြင်ဆင်မှု အလိုအလျောက် run အောင် လုပ်ထားပါ။
- Third-party package ရှာမယ့်အစား standard library မှာ ရှိပြီးသားလား အရင် စစ်ကြည့်ပါ။
Mini challenge
Mini challenge: Go fact card
Go အကြောင်း အချက်အလက် ကတ်လေး တစ်ခု print ထုတ်တဲ့ program ရေးပါ။ Package နှစ်ခု import လုပ်ပြီး နှစ်ခုလုံးကို သုံးရပါမယ်။
- `fmt` နဲ့ `strings` ကို ကွင်းထဲမှာ စုပြီး import လုပ်ရမယ်
- `strings.Repeat` နဲ့ မျဉ်းတစ်ကြောင်း ဆွဲရမယ်
- Language နာမည်၊ compiled ဖြစ်မဖြစ် (true) နဲ့ keyword အရေအတွက် (25) ကို print ထုတ်ရမယ်
- Run ပြီး import တစ်ခုကို ယာယီ ဖျက်ကြည့်ပြီး ဘာ error တက်လဲ မှတ်ထားပါ
package main
// TODO: grouped import of fmt and strings
func main() {
// TODO: build a line of 20 "-" characters with strings.Repeat
// TODO: print the line, then three facts, then the line again
}ရလဒ်: Output က ဒီလို ထွက်ပါမယ်: -------------------- Language: Go Compiled: true Keywords: 25 --------------------
အရိပ်အမြွက် ကြည့်ရန်
`strings.Repeat("-", 20)` က `-` နှစ်ဆယ်ပါတဲ့ string တစ်ခု ပြန်ပေးပါတယ်။ `fmt.Println("Compiled:", true)` လို boolean ကိုလည်း တိုက်ရိုက် print ထုတ်လို့ ရပါတယ်။
အဖြေ ကြည့်ရန်
package main
import (
"fmt"
"strings"
)
func main() {
line := strings.Repeat("-", 20)
fmt.Println(line)
fmt.Println("Language:", "Go")
fmt.Println("Compiled:", true)
fmt.Println("Keywords:", 25)
fmt.Println(line)
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Go က machine code အဖြစ် compile လုပ်ပြီး code၊ package နဲ့ runtime ပါတဲ့ executable တစ်ခုတည်း ထုတ်ပေးပါတယ်။
- go run က compile + run ကို တစ်ခါတည်း လုပ်ပြီး go build က executable ကို သိမ်းထားပေးပါတယ်။
- Unused import နဲ့ unused local variable တွေက compile error ဖြစ်ပါတယ်။
- Go မှာ class နဲ့ exception မရှိဘဲ struct, method နဲ့ error value တွေကို သုံးပါတယ်။
Go release history — Go