Thuta Learning
Go
BasicProgrammingအခြေခံ

Get Started (Setup)

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

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

  • Go ကို install လုပ်ပြီး terminal အသစ်မှာ go version နဲ့ စစ်နိုင်ဖို့
  • go mod init နဲ့ module တစ်ခု ဖန်တီးပြီး go.mod ထဲမှာ ဘာပါလဲ ဖတ်တတ်ဖို့
  • go run ., go run main.go နဲ့ go build ကွာခြားချက်ကို ရှင်းပြနိုင်ဖို့
  • 'go is not recognized', 'go.mod file not found' လို setup error တွေကို ကိုယ်တိုင် ပြင်နိုင်ဖို့

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

Setup ကို ရိုးရိုးလေး နားလည်မယ်

Go code ကို ကိုယ့်ကွန်ပျူတာမှာ run ဖို့ Go toolchain လိုပါတယ်။ Toolchain ဆိုတာ compiler၊ standard library နဲ့ `go` ဆိုတဲ့ command-line tool ကို ပေါင်းထားတဲ့ package တစ်ခုပါ။ တစ်ခါ install လုပ်ပြီးရင် `go run` နဲ့ code ကို တိုက်ရိုက် run နိုင်ပြီး `go build` နဲ့ executable file ထုတ်နိုင်ပါတယ်။

Project တစ်ခုစီကိုတော့ module လို့ ခေါ်တဲ့ folder တစ်ခုအဖြစ် ထားပါတယ်။ Module ရဲ့ ID ကတ်က `go.mod` file ပါ — project နာမည်နဲ့ သုံးမယ့် Go version ကို အဲဒီထဲမှာ မှတ်ထားပါတယ်။ အဲဒီ file ကို project တစ်ခုမှာ command တစ်ကြောင်းနဲ့ တစ်ကြိမ်ပဲ ဖန်တီးရပါတယ်။

Problem → Why → Solution

ပြဿနာ: code မှန်ပေမယ့် run လို့ မရ

Setup ပြဿနာ အများစုက code နဲ့ မဆိုင်ပါဘူး။ Terminal က home folder မှာ ရှိနေပြီး `main.go` က `hello` folder ထဲမှာ ရှိနေရင် `go run main.go` က file ကို ရှာမတွေ့ပါဘူး။ Folder မှန်ထဲ ဝင်ပြီး `go run .` လုပ်ပြန်တော့ `go.mod` မရှိသေးလို့ နောက်ထပ် error တစ်ခု ထွက်လာပါတယ်။

Error message တွေ မမြင်ဖူးသေးရင် code မှားနေတယ်လို့ ထင်ပြီး မှန်နေတဲ့ code ကို ပြင်နေမိတတ်ပါတယ်။

terminal
C:\Users\aye> go run main.go
CreateFile main.go: The system cannot find the file specified.

C:\Users\aye> cd hello
C:\Users\aye\hello> go run .
go: go.mod file not found in current directory or any parent directory;
see 'go help modules'

ဘာကြောင့် ဒီ error တွေ ဖြစ်လဲ

`go` command က terminal ရဲ့ လက်ရှိ folder ကနေ စပြီး file နဲ့ `go.mod` ကို ရှာပါတယ်။ ပထမ error က file ကို လက်ရှိ folder မှာ မတွေ့လို့ပါ (macOS နဲ့ Linux မှာ `stat main.go: no such file or directory` လို့ ပြပါတယ်)။ ဒုတိယ error ကတော့ `.` (ဒီ folder ထဲက package) ကို run ဖို့ module တစ်ခု လိုပြီး `go.mod` မရှိသေးလို့ပါ။ `go mod init` တစ်ကြိမ် run လိုက်ရင် ပြေလည်ပါတယ်။

အဓိက သဘောတရား: install ကနေ ပထမဆုံး build အထိ

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

1. Install လုပ်ပါ

Official site go.dev/dl ကနေ ကိုယ့် OS အတွက် installer (Windows မှာ .msi၊ macOS မှာ .pkg၊ Linux မှာ .tar.gz) ကို download လုပ်ပါ။ Installer က `go` ကို PATH ထဲ ထည့်ပေးပါတယ်။

2. Terminal အသစ်မှာ စစ်ပါ

Terminal အသစ် ဖွင့်ပြီး `go version` ကို run ပါ။ `go version go1.24.4 windows/amd64` လို version၊ OS နဲ့ CPU architecture ပါတဲ့ စာတစ်ကြောင်း ထွက်ရပါမယ်။

3. Module တစ်ခု စပါ

Project folder ထဲမှာ `go mod init example.com/hello` ကို run ပါ။ ဒါက `go.mod` file ကို ဖန်တီးပေးပါတယ်။ Module path က GitHub repository လိပ်စာလို ဘယ်နာမည်မဆို ဖြစ်နိုင်ပေမယ့် space မပါရပါဘူး။

4. Run ပါ

`main.go` ရေးပြီး `go run .` နဲ့ run ပါ။ `.` က 'ဒီ folder ထဲက package တစ်ခုလုံး' လို့ ဆိုလိုပါတယ်။

5. Build လုပ်ပါ

`go build` က module path ရဲ့ နောက်ဆုံးအပိုင်းကို နာမည်ပေးပြီး executable တစ်ခု ထုတ်ပါတယ် — Windows မှာ `hello.exe`၊ macOS နဲ့ Linux မှာ `hello` ပါ။

go.mod
module example.com/hello

go 1.24.4

`go.mod` ထဲမှာ module path နဲ့ `go` line ပါပါတယ်။ `go` line က module ဖန်တီးတုန်းက သုံးခဲ့တဲ့ toolchain version ကို မှတ်ထားပြီး ဒီ project အတွက် လိုအပ်တဲ့ အနည်းဆုံး Go version ကို ပြပါတယ်၊ ဒါကြောင့် သင့်စက်မှာ နံပါတ် ကွဲနိုင်ပါတယ်။ နောက်ပိုင်း third-party package သုံးတဲ့အခါ `require` line တွေနဲ့ `go.sum` file ပေါ်လာပါမယ်။ Modern Go မှာ module တွေကို default အဖြစ် သုံးလို့ code ကို GOPATH လို folder အထူးထဲ ထားစရာ မလိုတော့ပါဘူး။

Toolchain က ဘယ်မှာ ရှိလဲ သိချင်ရင် `go env` ကို သုံးပါ။ `go env GOROOT` က Go install လုပ်ထားတဲ့ folder ကို ပြပြီး `go env GOPATH` က download လုပ်ထားတဲ့ module source တွေနဲ့ `go install` နဲ့ ထည့်ထားတဲ့ tool တွေ သိမ်းတဲ့ folder ကို ပြပါတယ်၊ default က home folder ထဲက `go` folder ပါ။ ဒီ value တွေကို ပြင်စရာ မလိုသလောက်ပါပဲ၊ ဒါပေမယ့် `go install` နဲ့ ထည့်ထားတဲ့ tool ကို run မရတဲ့အခါ အဲဒီ folder ထဲက `bin` ကို PATH ထဲ ထည့်ဖို့ လိုတတ်ပါတယ်။

Setup အဆင့်လေးဆင့်
  1. go version အလုပ်လုပ်လား
  2. Folder မှန်ထဲ ရောက်နေလား
  3. go.mod ရှိလား
  4. go run . အလုပ်လုပ်လား

Setup error တစ်ခု ပေါ်ရင် ဒီ checkpoint တွေကို အစကနေ ပြန်စစ်ပါ၊ ပြဿနာ အများစုက ပထမ သုံးခုထဲမှာ ရှိပါတယ်။

Project အသစ် စတဲ့ command ပုံစံ

bash
mkdir project-name
cd project-name
go mod init example.com/project-name
# write main.go with package main and func main()
go run .
go build

Folder တစ်ခု ဖန်တီးပြီး အထဲ ဝင်ကာ module ကို init လုပ်ပါတယ်။ ပြီးရင် `main.go` ရေး၊ `go run .` နဲ့ စမ်း၊ ပို့ဆောင်ရမယ့်အခါ `go build` နဲ့ executable ထုတ်ပါတယ်။ Project အသစ်တိုင်းမှာ ဒီ အစဉ်အတိုင်း လုပ်ပါ။

Module ထဲက ပထမဆုံး main.go

`go mod init example.com/hello` run ပြီးတဲ့ folder ထဲမှာ ဒီ `main.go` ကို ရေးပြီး `go run .` နဲ့ run ပါ။ Program က setup အဆင့်တွေကို နံပါတ်တပ်ပြီး print ထုတ်ပါတယ်။ Slice နဲ့ `for` loop ကို နောက် lesson တွေမှာ အသေးစိတ် သင်ရမှာမို့ အခုတော့ 'step တစ်ခုချင်းအတွက် line တစ်ကြောင်း print ထုတ်တယ်' လို့ပဲ ဖတ်ထားပါ။

main.go
package main

import (
    "fmt"
    "strings"
)

func main() {
    steps := []string{"go version", "go mod init", "go run .", "go build"}

    fmt.Println("Setup checklist:")
    for i, step := range steps {
        fmt.Printf("%d. %s\n", i+1, step)
    }
    fmt.Println(strings.Repeat("=", 16))
    fmt.Println("Toolchain ready!")
}

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

  1. Two standard packages"strings"

    `fmt` နဲ့ `strings` နှစ်ခုလုံးက standard library ထဲမှာ ပါပြီးသားမို့ download လုပ်စရာ မလိုပါဘူး၊ `go.mod` ထဲမှာလည်း `require` line မပေါ်ပါဘူး။

  2. A list of stepssteps := []string{"go version", "go mod init", "go run .", "go build"}

    `[]string` က string တွေရဲ့ စာရင်း (slice) ပါ။ Command လေးခုကို အစဉ်လိုက် သိမ်းထားပါတယ်။

  3. Numbering each stepfor i, step := range steps {

    `range` က item တစ်ခုချင်းရဲ့ index (0 ကစ) နဲ့ value ကို ပေးပါတယ်။ ဒါကြောင့် နောက် line မှာ `i+1` နဲ့ 1 ကနေ စ နံပါတ်တပ်ပါတယ်။

  4. A separator linefmt.Println(strings.Repeat("=", 16))

    `strings.Repeat` က `=` ကို ၁၆ ကြိမ် ဆက်ထားတဲ့ string ပြန်ပေးပါတယ်။ `strings` ကို import လုပ်ထားတာ ဒီ line ကြောင့်ပါ — ဖျက်လိုက်ရင် unused import error ဖြစ်ပါမယ်။

You should see
Setup checklist:
1. go version
2. go mod init
3. go run .
4. go build
================
Toolchain ready!

go run main.go နဲ့ go run . ဘာကွာလဲ

Project ကြီးလာရင် `main` package ကို file တစ်ခုထက် ပိုခွဲရေးတတ်ပါတယ်။ `go run main.go` က နာမည်ပေးထားတဲ့ file တစ်ခုကိုပဲ compile လုပ်လို့ `helpers.go` ထဲက function ကို ခေါ်ရင် `undefined` error ရပါတယ်။ `go run .` ကတော့ folder ထဲက `.go` file အားလုံးကို package တစ်ခုအဖြစ် compile လုပ်ပါတယ်။ ဒါကြောင့် module ထဲမှာ `go run .` ကို အလေ့အကျင့်လုပ်ထားပါ။

Commandဘာလုပ်လဲ
go versionInstall လုပ်ထားတဲ့ version ကို ပြ
go mod init pathgo.mod ပါတဲ့ module အသစ် ဖန်တီး
go run .Folder ထဲက package ကို compile ပြီး run
go buildExecutable file ကို folder ထဲ ထုတ်
go fmt ./...File အားလုံးကို gofmt ပုံစံ ပြင်
go vet ./...Compile ဖြစ်ပေမယ့် သံသယဖြစ်စရာ code ကို ရှာ

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

လက်တွေ့ team တွေမှာ setup ကို ဘယ်လို လုပ်လဲ

Team project တွေမှာ developer အသစ်တစ်ယောက် ရောက်လာရင် repository ကို clone လုပ်ပြီး `go build ./...` run ရုံနဲ့ build ဖြစ်ရပါမယ်။ `go.mod` နဲ့ `go.sum` ကို git ထဲ commit လုပ်ထားလို့ dependency version တွေ လူတိုင်းမှာ တူနေပါတယ်။ Go 1.21 နဲ့ နောက်ပိုင်းမှာ `go.mod` ထဲက `go` နဲ့ `toolchain` line တွေအရ လိုအပ်တဲ့ toolchain ကို `go` command က ကိုယ်တိုင် download လုပ်ပေးနိုင်ပါတယ်။

CI pipeline တွေကလည်း ဒီ lesson ထဲက command တွေကိုပဲ အစဉ်လိုက် run ပါတယ် — `go vet ./...`၊ `go test ./...`၊ ပြီးရင် `go build`။ Linux server အတွက် build လုပ်ဖို့ `GOOS=linux GOARCH=amd64 go build` လို environment variable နှစ်ခု သတ်မှတ်ရုံပါပဲ။ ဒါကြောင့် ကိုယ့်စက်မှာ setup ကို သေချာ နားလည်ထားရင် production build process ကိုလည်း နားလည်ပြီးသား ဖြစ်ပါတယ်။

အမှားများ

Setup မှာ အဖြစ်များတဲ့ အမှားတွေ

1. Install ပြီး terminal အဟောင်းကို ဆက်သုံးတာ

Install မလုပ်ခင်က ဖွင့်ထားတဲ့ terminal က PATH အသစ်ကို မသိလို့ Windows မှာ `'go' is not recognized as an internal or external command`၊ macOS/Linux မှာ `go: command not found` ပြပါတယ်။ Terminal ကို ပိတ်ပြီး အသစ်ဖွင့်ပါ၊ VS Code ထဲက terminal ဆိုရင် VS Code ကိုပါ ပြန်ဖွင့်ပါ။

2. Folder မှားပြီး run တာ

`go run main.go` က လက်ရှိ folder ထဲမှာပဲ ရှာပါတယ်။ Run မလုပ်ခင် Windows မှာ `dir`၊ macOS/Linux မှာ `ls` နဲ့ `main.go` မြင်ရလား စစ်ပြီး မမြင်ရရင် `cd` နဲ့ project folder ထဲ ဝင်ပါ။

3. go.mod မဖန်တီးဘဲ go run . လုပ်တာ

`go run .` နဲ့ `go build` က module တစ်ခုထဲမှာ အလုပ်လုပ်ဖို့ ရည်ရွယ်ထားလို့ `go.mod` မရှိရင် `go.mod file not found` error ပေးပါတယ်။ Project folder တိုင်းမှာ `go mod init` ကို တစ်ကြိမ် run ပါ။ Folder တစ်ခုထဲမှာ ထပ် run ရင်တော့ go.mod ရှိပြီးသားလို့ ငြင်းပါလိမ့်မယ်။

Mini challenge

Mini challenge: setup report

`setupcheck` ဆိုတဲ့ module အသစ်တစ်ခု ဖန်တီးပြီး ကိုယ့်စက်ရဲ့ Go version၊ OS၊ CPU architecture နဲ့ CPU core အရေအတွက်ကို print ထုတ်တဲ့ program ရေးပါ။

  • Folder အသစ်မှာ `go mod init example.com/setupcheck` run ရမယ်
  • Standard library ထဲက `runtime` package ကို သုံးရမယ်
  • `runtime.Version()`, `runtime.GOOS`, `runtime.GOARCH`, `runtime.NumCPU()` ကို print ထုတ်ရမယ်
  • `go run .` နဲ့ run ပြီး `go build` နဲ့ executable ပါ ထုတ်ကြည့်ရမယ်
main.go
package main

import "fmt" // TODO: turn this into a grouped import that adds runtime

func main() {
    // TODO: print a "Setup report" heading
    // TODO: print the Go version (a function call)
    // TODO: print GOOS and GOARCH joined with "/" (constants)
    // TODO: print the number of CPU cores (a function call)
}

ရလဒ်: စက်ပေါ် မူတည်ပြီး ကွဲပါမယ်၊ ဥပမာ: Setup report Go version: go1.24.4 OS/Arch: windows/amd64 CPU cores: 8

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

`runtime.GOOS` နဲ့ `runtime.GOARCH` က string constant တွေဖြစ်လို့ `+ "/" +` နဲ့ ဆက်လို့ရပါတယ်။ `Version()` နဲ့ `NumCPU()` ကတော့ function တွေမို့ ကွင်းနဲ့ ခေါ်ရပါတယ်။

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

import (
    "fmt"
    "runtime"
)

func main() {
    fmt.Println("Setup report")
    fmt.Println("Go version:", runtime.Version())
    fmt.Println("OS/Arch:", runtime.GOOS+"/"+runtime.GOARCH)
    fmt.Println("CPU cores:", runtime.NumCPU())
}

Quick Quiz 1 / 3

Go install ပြီးချက်ချင်း အရင်ဖွင့်ထားတဲ့ terminal မှာ go version run တော့ 'go' is not recognized ပြတယ်။ အဖြစ်နိုင်ဆုံး ဖြေရှင်းနည်းက ဘာလဲ။

Quick Quiz 2 / 3

Module path က example.com/hello ဖြစ်ပြီး Windows ပေါ်မှာ go build (argument မပါ) run ရင် ဘာ file ထွက်မလဲ။

Quick Quiz 3 / 3

main.go နဲ့ helpers.go နှစ်ခုလုံး package main ဖြစ်ပြီး main.go က helpers.go ထဲက function ကို ခေါ်ထားတယ်။ go run main.go က ဘာဖြစ်မလဲ။

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

  • Go toolchain ကို go.dev/dl ကနေ install လုပ်ပြီး terminal အသစ်မှာ go version နဲ့ စစ်ပါ။
  • Project တိုင်းမှာ go mod init နဲ့ module စပြီး go.mod က module path နဲ့ Go version ကို မှတ်ထားပါတယ်။
  • go run . က folder ထဲက package တစ်ခုလုံးကို run ပြီး go build က executable ထုတ်ပါတယ်။
  • Setup error တွေက များသောအားဖြင့် PATH၊ folder မှားတာ ဒါမှမဟုတ် go.mod မရှိတာကြောင့်ပါ။

Go release history — Go

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

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

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

Get Started (Setup) | Thuta Learning