Thuta Learning
Go
BasicProgrammingအခြေခံ

Go HOME

Goသင်ခန်းစာ 115 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Go က ဘယ်လို language မျိုးလဲ (compiled, statically typed, garbage-collected, concurrency built-in) နဲ့ ဘယ်လို software တွေမှာ သုံးလဲ ပြောပြနိုင်ဖို့
  • Basic, Intermediate, Advanced, Projects, Exercises chapter ငါးခုမှာ ဘာတွေ သင်ရမလဲ သိဖို့
  • Run လို့ရတဲ့ Go file တိုင်းရဲ့ အပိုင်းသုံးပိုင်း (package main, import, func main) ကို မှတ်မိဖို့
  • go run နဲ့ file တစ်ခုကို run ပြီး ကြိုမှန်းထားတဲ့ output နဲ့ တိုက်စစ်တတ်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

Go ကို ရိုးရိုးလေး ပြောရရင်

Go (Golang လို့လည်း ခေါ်ပါတယ်) ဆိုတာ Google မှာ Robert Griesemer, Rob Pike နဲ့ Ken Thompson တို့ design လုပ်ခဲ့ပြီး 2009 မှာ open source အဖြစ် ထုတ်ပြန်ခဲ့တဲ့ programming language တစ်ခုပါ။ သင်ရေးတဲ့ `.go` file ကို Go compiler က ကွန်ပျူတာ တိုက်ရိုက် run နိုင်တဲ့ machine code အဖြစ် ပြောင်းပေးလို့ program က မြန်မြန် စ run ပြီး မြန်မြန် အလုပ်လုပ်ပါတယ်။

Go ရဲ့ ထူးခြားချက်က ရိုးရှင်းမှုပါ။ Keyword အရေအတွက် နည်းပြီး အလုပ်တစ်ခုကို ရေးနည်း တစ်နည်း နှစ်နည်းလောက်ပဲ ရှိတာကြောင့် တခြားသူ ရေးထားတဲ့ code ကို ဖတ်ရတာ လွယ်ပါတယ်။ ဒါကြောင့် API server, command-line tool, cloud နဲ့ DevOps tool တွေမှာ အရမ်း အသုံးများပါတယ်။

Problem → Why → Solution

ပြဿနာ: အဆင့်ကျော်ပြီး ခက်တဲ့ code ကို အရင် copy ကူးတာ

Go ကို စလေ့လာတဲ့သူ အများစုက goroutine တွေ အကြောင်း ကြားဖူးလို့ internet ပေါ်က concurrency code ကို တန်းပြီး copy ကူး run ကြည့်တတ်ပါတယ်။ အောက်က program က goroutine တစ်ခု စတင်ပြီး စာတစ်ကြောင်း print ထုတ်ခိုင်းထားပါတယ်။ Error မတက်ပါဘူး၊ ဒါပေမယ့် run လိုက်တဲ့အခါ အများအားဖြင့် ဘာမှ print မထွက်ပါဘူး။

Function, goroutine, program ဘယ်အချိန် ပြီးဆုံးလဲ ဆိုတာ မသိသေးရင် ဒီ code ဘာကြောင့် ဘာမှ မထုတ်လဲ ဆိုတာ ရှင်းပြလို့ မရပါဘူး။ အဖြေကို ရှာရင်းနဲ့ လမ်းပျောက်ပြီး Go က ခက်တယ်လို့ အထင်မှားသွားတတ်ပါတယ်။

main.go
package main

import "fmt"

func main() {
    go fmt.Println("hello from a goroutine")
    // main returns here, and the whole program exits with it,
    // usually before the goroutine has had a chance to print.
}

ဘာကြောင့် အစဉ်လိုက် သင်ရတာလဲ

Go ရဲ့ feature တွေက တစ်ခုပေါ် တစ်ခု ဆင့်ပြီး တည်ဆောက်ထားပါတယ်။ Goroutine ကို နားလည်ဖို့ function လိုပြီး channel ကို နားလည်ဖို့ type တွေ လိုပါတယ်။ Error handling ကလည်း interface ပေါ်မှာ အခြေခံပါတယ်။ ဒီ course ကို အဲဒီ အစဉ်အတိုင်း စီထားလို့ lesson တစ်ခုချင်းက ရှေ့ lesson တွေကို အသုံးပြုပါတယ်။ အပေါ်က program ကို Advanced chapter ရဲ့ Goroutines lesson ရောက်တဲ့အခါ ကိုယ်တိုင် ပြင်နိုင်သွားပါမယ်။

အဓိက သဘောတရား: Go ဆိုတာ ဘာလဲ၊ ဒီ course ကို ဘယ်လို စီထားလဲ

Go ကို ဂုဏ်သတ္တိ လေးခုနဲ့ မှတ်ထားနိုင်ပါတယ်။ ပထမ၊ compiled language ဖြစ်လို့ code ကို run မတိုင်ခင် machine code အဖြစ် ပြောင်းပြီး executable file တစ်ခုတည်း ထွက်လာပါတယ်။ ဒုတိယ၊ statically typed ဖြစ်လို့ variable တိုင်းမှာ type ရှိပြီး type မကိုက်တဲ့ အမှားတွေကို compiler က run မတိုင်ခင် ဖမ်းပေးပါတယ်။

တတိယ၊ garbage collector ပါလို့ memory ကို ကိုယ်တိုင် free လုပ်စရာ မလိုပါဘူး။ စတုတ္ထ၊ goroutine နဲ့ channel ဆိုတဲ့ concurrency feature တွေက language ထဲမှာကိုပဲ ပါပြီးသားမို့ အလုပ်အများကြီးကို တစ်ပြိုင်နက် လုပ်တဲ့ server တွေ ရေးရတာ လွယ်ပါတယ်။ ဒါ့အပြင် `go` ဆိုတဲ့ command တစ်ခုတည်းမှာ run, build, test, format, vet စတဲ့ tool တွေ အကုန် ပါပါတယ်။

ဒီ course မှာ lesson ၃၂ ခုကို chapter ငါးခု ခွဲထားပါတယ်။ Chapter တစ်ခုချင်းက နောက် chapter အတွက် လိုတဲ့ အုတ်မြစ်ကို ချပေးပါတယ်။

Chapterဘာတွေ သင်မလဲ
Basic (lesson ၆ ခု)Go ဆိုတာ ဘာလဲ၊ setup၊ syntax၊ variable နဲ့ data type
Intermediate (lesson ၈ ခု)Function, array, slice, map, loop, struct, method, interface
Advanced (lesson ၁၃ ခု)Generics, pointer, error, module, goroutine, channel, context, HTTP, testing နဲ့ mini project
Projects (lesson ၃ ခု)Task Manager ကို struct နဲ့ slice ကနေ JSON auto-save အထိ အပိုင်းသုံးပိုင်းနဲ့ တည်ဆောက်
Exercises (lesson ၂ ခု)အခြေခံ review နဲ့ interface၊ concurrency challenge

Lesson တစ်ခုချင်းကိုလည်း ပုံစံတူ စီထားပါတယ်: ရိုးရိုး ရှင်းပြချက်၊ ပြဿနာ၊ core concept၊ example code နဲ့ line-by-line ရှင်းပြချက်၊ အဖြစ်များတဲ့ အမှားတွေ၊ mini challenge နဲ့ quiz သုံးခု။ Example တိုင်းက `package main` နဲ့ `func main()` ပါတဲ့ file တစ်ခုတည်း ဖြစ်လို့ `go run main.go` နဲ့ တိုက်ရိုက် run လို့ ရပါတယ်။

Go ဟာ backward compatibility ကို အလေးထားတဲ့ language ဖြစ်ပါတယ်။ Go 1 compatibility promise အရ Go 1 အတွက် မှန်မှန်ကန်ကန် ရေးထားတဲ့ program တွေက Go 1.x version အသစ်တွေမှာလည်း ဆက် compile ဖြစ်ဖို့ ရည်ရွယ်ထားပါတယ်။ ဒါကြောင့် ဒီ course မှာ သင်တဲ့ အခြေခံတွေက နှစ်ပေါင်းများစွာ အသုံးဝင်နေပါမယ်။ Version အချို့မှာမှ ပါတဲ့ feature တွေကိုတော့ 'Go 1.22 နဲ့ နောက်ပိုင်း' လို ရေးပြီး သတိပေးထားပါမယ်။

Lesson တစ်ခုကို လေ့လာနည်း
  1. ရှင်းပြချက်ကို ဖတ်
  2. Output ကို ကြိုမှန်း
  3. go run နဲ့ run ကြည့်
  4. တစ်နေရာ ပြောင်းပြီး ပြန် run

ကြိုမှန်းတာနဲ့ တကယ်ထွက်တာ မတူရင် အဲဒီနေရာက သင် အခုမှ သင်ယူရမယ့် အချက်ပါ။

Run လို့ရတဲ့ Go file ရဲ့ ပုံစံ

main.go
package main

import "fmt"

func main() {
    name := value
    fmt.Println(name)
}

`package main` က ဒီ file ဟာ run လို့ရတဲ့ program ဖြစ်ကြောင်း ပြောပါတယ်။ `import` က သုံးမယ့် package တွေကို ခေါ်ပြီး `func main()` ထဲက statement တွေကို အပေါ်ကနေ အောက်ကို အစဉ်လိုက် run ပါတယ်။ ဒီ course ရဲ့ example တိုင်းက ဒီ အရိုးစု ပုံစံကို လိုက်ပါတယ်။

ပထမဆုံး Go program: course progress report

အောက်က program က course အချက်အလက်တွေကို variable ထဲ သိမ်းပြီး ကျန်တဲ့ lesson အရေအတွက်ကို တွက်ပြပါတယ်။ `main.go` အဖြစ် save လုပ်ပြီး `go run main.go` နဲ့ run ကြည့်ပါ။ Run မလုပ်ခင် output လေးကြောင်းကို ကြိုမှန်းကြည့်ပါ၊ အထူးသဖြင့် `Println` က item တွေကြားမှာ space ထည့်ပေးတာကို သတိထားပါ။

main.go
package main

import "fmt"

func main() {
    course := "Go"
    totalLessons := 32
    chapters := 5
    finished := 1

    fmt.Println("Welcome to the", course, "course!")
    fmt.Printf("%d lessons in %d chapters\n", totalLessons, chapters)
    fmt.Println("Lessons left:", totalLessons-finished)
    fmt.Println("Next lesson: What is Go?")
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. An executable programpackage main

    Go file တိုင်းက package clause နဲ့ စပါတယ်။ `main` ဆိုတဲ့ package နာမည်က compiler ကို 'ဒါ library မဟုတ်ဘူး၊ run လို့ရတဲ့ program ပါ' လို့ ပြောတာပါ။

  2. Values with inferred typestotalLessons := 32

    `:=` က variable အသစ်ကို ကြေညာပြီး value ပေးတာပါ။ `32` ကို ကြည့်ပြီး compiler က type ကို `int` လို့ သတ်မှတ်ပေးပါတယ်။ Variables lesson မှာ အသေးစိတ် လေ့လာပါမယ်။

  3. Println adds spacesfmt.Println("Welcome to the", course, "course!")

    `Println` က argument တွေကြားမှာ space တစ်ခုစီ ထည့်ပြီး အဆုံးမှာ newline ထည့်ပါတယ်။ ဒါကြောင့် `Welcome to the Go course!` လို့ စာလုံးတွေ ကပ်မနေဘဲ ထွက်လာပါတယ်။

  4. Formatted outputfmt.Printf("%d lessons in %d chapters\n", totalLessons, chapters)

    `Printf` က format string ထဲက `%d` နေရာတွေမှာ integer တွေကို အစဉ်လိုက် ထည့်ပါတယ်။ `Println` နဲ့ မတူတာက newline ကို အလိုအလျောက် မထည့်လို့ `\n` ကို ကိုယ်တိုင် ရေးရတာပါ။

  5. Arithmetic inside a callfmt.Println("Lessons left:", totalLessons-finished)

    `totalLessons-finished` ကို အရင် တွက်လို့ `31` ရပြီးမှ print ထုတ်ပါတယ်။ Go မှာ string နဲ့ number ကို `+` နဲ့ ပေါင်းလို့ မရလို့ comma နဲ့ ခွဲပြီး argument သီးသီး ပို့ရပါတယ်။

You should see
Welcome to the Go course!
32 lessons in 5 chapters
Lessons left: 31
Next lesson: What is Go?
main.go ကနေ output အထိ
  1. main.go ကို save လုပ်တယ်
  2. go run က compile လုပ်တယ်
  3. Program က main() ကို run တယ်
  4. Output လေးကြောင်း ထွက်လာတယ်

Code မှန်တယ်

A temporary executable runs and prints the report

Compile error ရှိတယ်

file:line:column plus a message; nothing runs

လက်တွေ့ project တွေမှာ

Go ကို လက်တွေ့ ဘယ်မှာ သုံးလဲ

Cloud infrastructure အများစုက Go နဲ့ ရေးထားပါတယ်။ Docker, Kubernetes, Terraform, Prometheus, etcd နဲ့ static site generator Hugo တို့ဟာ Go နဲ့ ရေးထားတဲ့ project တွေပါ။ ဒီ tool တွေ Go ကို ရွေးရတဲ့ အကြောင်းရင်းက executable file တစ်ခုတည်း ထွက်လာလို့ server ပေါ် copy ကူးပြီး run ရုံပဲ ဖြစ်တာ၊ goroutine တွေကြောင့် network request ထောင်ချီကို တစ်ပြိုင်နက် ကိုင်တွယ်နိုင်တာနဲ့ compile မြန်တာတို့ ဖြစ်ပါတယ်။

ကုမ္ပဏီတွေမှာလည်း Go ကို backend API, microservice, data pipeline နဲ့ team အတွင်း သုံးတဲ့ command-line tool တွေအတွက် အသုံးများပါတယ်။ ဒီ course ရဲ့ project တွေက အဲဒီ အလုပ်အမျိုးအစားတွေကို အသေးစား ပုံစံနဲ့ ပြန်လုပ်ကြည့်တာပါ — Task Manager project က JSON file ထဲ data သိမ်းတဲ့ CLI tool ဖြစ်ပြီး HTTP & JSON API lesson က backend service ရဲ့ အခြေခံကို ပြပါတယ်။ Course ပြီးတဲ့အခါ အဲဒီလို tool အသေးလေးတွေကို ကိုယ်တိုင် ရေးနိုင်ပါပြီ။

အမှားများ

အစပိုင်းမှာ အဖြစ်များတဲ့ အမှားတွေ

1. Code ကို ဖတ်ရုံပဲ ဖတ်ပြီး run မကြည့်တာ

Example ကို ဖတ်ပြီး 'နားလည်ပါပြီ' လို့ ထင်ရပေမယ့် ကိုယ်တိုင် run ကြည့်မှ `Println` ရဲ့ space တွေ၊ `Printf` မှာ `\n` မေ့တာမျိုး အသေးစိတ်တွေကို တွေ့ရပါတယ်။ Example တိုင်းကို copy ကူး run ပြီး value တစ်ခု ပြောင်းကာ ပြန် run ကြည့်ပါ။

2. Compile error message ကို မဖတ်ဘဲ ကျော်တာ

`./main.go:9:5: declared and not used: name` လို message မှာ file နာမည်၊ line 9၊ column 5 နဲ့ ပြဿနာ အတိအကျ ပါပါတယ်။ Go compiler ရဲ့ message တွေက တိုပြီး တိကျလို့ error တစ်ခု ပေါ်ရင် အဲဒီ line ကို တန်းသွားကြည့်ပါ၊ code တစ်ခုလုံးကို ခန့်မှန်းပြီး ပြင်မနေပါနဲ့။

3. Advanced topic တွေဆီ အရင် ခုန်သွားတာ

Goroutine နဲ့ channel တွေက Go ရဲ့ ဆွဲဆောင်မှုပေမယ့် function, slice, struct, interface တွေကို မသိဘဲ ရေးရင် ပြဿနာ ပေါ်တဲ့အခါ ဘာကြောင့်လဲ ရှင်းလို့ မရပါဘူး။ Basic နဲ့ Intermediate chapter တွေကို အရင်ပြီးအောင် လုပ်ပါ၊ နောက်ပိုင်း lesson တွေက အဲဒါတွေကို သိပြီးသားလို့ ယူဆထားပါတယ်။

Best Practices

လေ့လာရာမှာ အကောင်းဆုံး အလေ့အကျင့်များ

  • Lesson တစ်ခုချင်းအတွက် folder သီးသန့် ထားပြီး `main.go` file တစ်ခုနဲ့ စမ်းပါ။
  • Mini challenge ကို solution မကြည့်ခင် ကိုယ်တိုင် အရင် ကြိုးစားပါ၊ hint ကိုလည်း လိုမှ ဖွင့်ပါ။
  • Quiz မှာ မှားတဲ့ မေးခွန်းရဲ့ explanation ကို ဖတ်ပြီး example ကို ပြန် run ကြည့်ပါ။

Mini challenge

Mini challenge: ကိုယ့် study plan ကို print ထုတ်ပါ

ဒီ course ကို တစ်ပတ်ကို lesson ဘယ်နှစ်ခု လေ့လာမလဲ ဆုံးဖြတ်ပြီး plan ကို print ထုတ်တဲ့ program တစ်ခု ရေးပါ။

  • ကိုယ့်နာမည်၊ စုစုပေါင်း lesson (32) နဲ့ တစ်ပတ် lesson အရေအတွက် (4) ကို `:=` နဲ့ variable တွေထဲ သိမ်းရမယ်
  • Basic chapter (lesson 6 ခု) ပြီးရင် ကျန်မယ့် lesson အရေအတွက်ကို တွက်ရမယ်
  • Course တစ်ခုလုံး ပြီးဖို့ ဘယ်နှစ်ပတ် ကြာမလဲ တွက်ရမယ်
  • `Println` နဲ့ `Printf` နှစ်ခုလုံးကို အနည်းဆုံး တစ်ကြိမ်စီ သုံးရမယ်
main.go
package main

import "fmt"

func main() {
    // TODO: learner name, total lessons, lessons per week
    // TODO: the Basic chapter has 6 lessons

    // TODO: print the learner's name with Println
    // TODO: print lessons left after Basic
    // TODO: print the number of weeks with Printf and %d
}

ရလဒ်: Learner: Aye Aye Left after Basic: 26 Weeks to finish: 8

အရိပ်အမြွက် ကြည့်ရန်

Integer နှစ်ခုကို `/` နဲ့ စားရင် Go က ဒသမကို ဖြတ်ပစ်ပြီး integer ပဲ ပြန်ပေးပါတယ်။ 32 / 4 က တိတိ 8 ဖြစ်လို့ ဒီ challenge မှာ ပြဿနာ မရှိပါဘူး။

အဖြေ ကြည့်ရန်
main.go
package main

import "fmt"

func main() {
    learner := "Aye Aye"
    total := 32
    perWeek := 4
    basic := 6

    fmt.Println("Learner:", learner)
    fmt.Println("Left after Basic:", total-basic)
    fmt.Printf("Weeks to finish: %d\n", total/perWeek)
}

Quick Quiz 1 / 3

Go program တစ်ခု run တဲ့အခါ ဘယ်နေရာကနေ စပြီး အလုပ်လုပ်လဲ။

Quick Quiz 2 / 3

total := 32 နဲ့ done := 1 ရှိရင် fmt.Println("Lessons left:", total-done) က ဘာ print ထုတ်မလဲ။

Quick Quiz 3 / 3

ဒီ course မှာ goroutine နဲ့ channel ကို ဘယ် chapter မှာ သင်မလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Go က compiled, statically typed, garbage-collected language ဖြစ်ပြီး goroutine နဲ့ channel ကို language ထဲမှာ ပါပြီးသားပါ။
  • ဒီ course မှာ lesson ၃၂ ခုကို Basic, Intermediate, Advanced, Projects, Exercises ဆိုတဲ့ chapter ငါးခု အစဉ်လိုက် စီထားပါတယ်။
  • Run လို့ရတဲ့ file တိုင်းမှာ package main၊ import နဲ့ func main() ပါပြီး go run main.go နဲ့ run ပါတယ်။
  • Example တိုင်းကို output ကြိုမှန်းပြီး run ကြည့်၊ တစ်နေရာ ပြောင်းပြီး ပြန် run ပါ။

Go release history — Go

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Go HOME | Thuta Learning