Reference · Day 1

Go Day 1 — Cheat Sheet

ทำไมต้อง Go · ติดตั้ง · module · โครงสร้างโปรแกรม · ตัวแปรและ type · ตัวอย่างรันจริงบน Go 1.26 แล้ว

สารบัญ

  1. ทำไมต้อง Go
  2. ติดตั้ง (mise + VS Code)
  3. Go modules
  4. โครงสร้างโปรแกรม
  5. go run / build / fmt / vet
  6. ตัวแปร
  7. Type
  8. fmt verbs
  9. กับดัก Day 1

1 · ทำไมต้อง Go

ภาษาจุดขาย
C++Control, Hardware
PythonProductivity, Fast iteration
JavaPortability, Managed runtime
GoEngineering Scale, Cloud & Concurrency

ประวัติ: เกิดที่ Google ปี 2007 โดย Robert Griesemer, Rob Pike, Ken Thompson ต้องการภาษาที่ได้ครบ 3 อย่างพร้อมกัน — compile เร็ว · รันเร็ว · เขียนง่าย หาภาษาที่มีครบไม่เจอ เลยสร้างเอง

Go หรือ Golang? ชื่อทางการคือ Go — ที่เรียก Golang เพราะตอนจดโดเมนใช้ go เฉย ๆ ไม่ได้ เลยใช้ golang.org (ปัจจุบันย้ายมา go.dev แล้ว)

Less is Exponentially More — 25 keywords ทั้งภาษา

โครงสร้าง/ประกาศ ชนิดข้อมูล เงื่อนไข Loop/flow Lifecycle package struct if for defer import interface else range go const map switch break var chan case continue type default fallthrough func select goto return

เทียบกับ Java (~50) หรือ C++ (~90) — น้อยกว่า = จำน้อยกว่า = โค้ดของทุกคนหน้าตาเหมือนกัน และเป็นเหตุผลที่ AI generate โค้ด Go ได้แม่นกว่าภาษาอื่น (syntax เล็ก, static type, go fmt บังคับ style)

Performance (TechEmpower, PostgreSQL raw)

Java 1,040,599 (Vert.x) Go 959,399 (fasthttp-prefork) ← ตามหลัง Java นิดเดียว Node.js 265,826 (Fastify) ← Go เร็วกว่า ~3.6 เท่า Python 109,166 (FastAPI) ← Go เร็วกว่า ~8.8 เท่า

⚠️ อย่าอ่าน benchmark เป็นตัวเลขเดียวจบ — performance มีหลายมิติ: throughput, latency, resource usage, startup/warm-up ตัวเลขนี้คือ throughput อย่างเดียว ที่ Go ชนะขาดจริงคือ startup time + memory ซึ่งสำคัญกับ container

Go อยู่ตรงไหนของโลกจริง

หมวดเครื่องมือที่เขียนด้วย Go
Container & OrchestrationDocker, Kubernetes
Monitoring & LoggingPrometheus, Grafana Loki
IaC & SecretsTerraform, Vault, Consul
Service Mesh & NetworkIstio, Traefik
GitOpsArgoCD
StorageMinIO

บริษัทที่ใช้: Google, Meta, Microsoft, Netflix, Uber, PayPal, Cloudflare, Dropbox, Twitch, Grab, LINE, Agoda, ByteDance, Monzo, American Express

2 · ติดตั้ง

VS Code

ลง extension ชื่อ Go (id: golang.go, publisher: Go Team at Google)

mise — จัดการเวอร์ชัน Go

# ติดตั้ง mise (Linux / macOS / WSL)
curl https://mise.run | sh
mise --version

# เปิดใช้งานใน shell
echo 'eval "$(~/.local/bin/mise activate zsh)"' >> ~/.zshrc   # zsh
echo 'eval "$(~/.local/bin/mise activate bash)"' >> ~/.bashrc  # bash

# ตั้ง Go เป็น global
mise use --global go@1.26
go version

# หรือกำหนดเฉพาะโปรเจกต์ (สร้างไฟล์ mise.toml)
mise use go@1.26

# รันโดยไม่ต้องติดตั้งถาวร
mise exec go@1.26 -- go version
# mise.toml
[tools]
go = "1.26.4"

ประโยชน์: คนละโปรเจกต์ใช้ Go คนละเวอร์ชันได้ และเพื่อนร่วมทีมได้เวอร์ชันเดียวกันอัตโนมัติเพราะ mise.toml อยู่ใน repo

3 · Go modules

go-to-hero/ ├── go.mod ← module path + Go version + dependencies ├── go.sum ← checksum กันคนสลับของ └── main.go
mkdir go-to-hero && cd go-to-hero

go mod init go-to-hero                              # ชื่อสั้น
go mod init gitlab.com/username/go-to-hero          # ชื่อเต็ม (ถ้าจะเผยแพร่)

go mod tidy      # เพิ่มของที่ใช้ + ลบของที่ไม่ใช้ ← รันบ่อย ๆ

Module = ชุดของ package ที่ release/version/distribute ไปด้วยกัน

4 · โครงสร้างโปรแกรม

package main          // ชื่อ main = executable / ชื่ออื่น = library

import (
    "fmt"
    "math/rand"       // เรียกใช้ด้วย rand.Intn() ← ชิ้นสุดท้ายของ path
)

func init() { ... }   // รันก่อน main เสมอ · มีหลายตัวได้ · เรียกเองไม่ได้

func main() { ... }   // จุดเริ่มต้น · ไม่รับ param ไม่คืนค่า · มีตัวเดียว

ลำดับการทำงาน

1. import package ทั้งหมด (รัน init ของ package นั้น ๆ ก่อน) 2. ประกาศตัวแปรระดับ package 3. init() ← ตามลำดับที่เขียน 4. main()

ใช้ init() เท่าที่จำเป็น — มันรันเงียบ ๆ โดยไม่มีใครเรียก ทำให้ไล่โค้ดยาก ถ้าเขียนเป็นฟังก์ชันธรรมดาแล้วเรียกจาก main ได้ ให้ทำแบบนั้น

5 · เครื่องมือ

คำสั่งทำอะไร
go run .compile ชั่วคราว → รัน → ลบทิ้ง (ตอนพัฒนา)
go buildcompile เป็น binary เก็บไว้ (ตอน deploy)
go fmt ./...จัดรูปแบบโค้ดให้เป็นมาตรฐานเดียวกันทั้งโลก
go vet ./...หาโค้ดที่ compile ผ่านแต่น่าจะผิด
go mod tidyจัดระเบียบ dependency
go build -gcflags="-m"ดูว่าตัวแปรไหน escape ไป heap
go run . go build main.go source + dependencies ↓ ↓ load package Go compiler ↓ ↓ compile → binary ชั่วคราว Linker ↓ ↓ รัน → ลบทิ้ง binary จริง เก็บบนดิสก์

Cross-compilation — จุดขายใหญ่ของ Go

GOOS=linux GOARCH=amd64 go build -o bin/app
GOOSGOARCH
windows · darwin (macOS) · linux · freebsd · android · ios · js (WebAssembly) amd64 (64-bit x86) · 386 (32-bit x86) · arm64 · arm · riscv64 · wasm

ได้ไฟล์เดียวจบ ไม่ต้องลง runtime ที่ปลายทาง — เหตุผลที่ Go ครองโลก DevOps

6 · ตัวแปร

var i int                          // zero value → 0
var j int = 2
var c, python, java = true, false, "no!"
k := 3                              // ในฟังก์ชันเท่านั้น!
name, age := "Alice", 30
สถานการณ์ใช้
ในฟังก์ชัน มีค่าเริ่มต้น:= ← สำนวนมาตรฐาน
ต้องการ zero valuevar x int
นอกฟังก์ชัน (package level)var เท่านั้น
อยากบังคับ type ให้ชัดvar x float64 = 3

Zero values

TypeZero value
ตัวเลขทุกชนิด0
boolfalse
string"" (ไม่ใช่ nil — string เป็น nil ไม่ได้)
pointer, slice, map, func, chan, interfacenil

Go ไม่มี undefined — ตัวแปรพร้อมใช้เสมอ ไม่ต้องเช็ค null ก่อน

7 · Type

bool
string

int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte  // = uint8
rune  // = int32 — Unicode code point (ตัวอักษร 1 ตัว)

float32 float64
complex64 complex128
กฎง่าย ๆ ที่ใช้ได้ 95%

ใช้ int กับ float64 จนกว่าจะมีเหตุผลเฉพาะ

Typeช่วงใช้ตอน
int8−128 ถึง 127protocol, binary data
int32±2.1 พันล้านfixed-size field, Unicode rune
int64±9.22e18timestamp, database ID, file size
uint0 ขึ้นไปcount/index เฉพาะกรณีที่ต้อง unsigned
uint80 ถึง 255byte data, color channel, raw binary

Type conversion — T(v)

i := 42
f := float64(i)
u := uint(f)

var x, y int = 3, 4
var dist float64 = math.Sqrt(float64(x*x + y*y))

Go ไม่แปลง type ให้อัตโนมัติ — int + float64 = compile error

Type inference — default type ของค่าคงที่

n := 42      // int      (ไม่ใช่ int64)
f := 3.142   // float64  (ไม่ใช่ float32)
s := "hi"    // string
b := true    // bool

8 · fmt verbs ที่ใช้บ่อย

verbพิมพ์ตัวอย่าง
%vค่าแบบ default{Alice 30}
%+vค่า + ชื่อ field{Name:Alice Age:30}
%Ttype — เครื่องมือดีบักอันดับ 1main.User
%dจำนวนเต็ม42
%sสตริงhi
%qสตริงพร้อม quote"hi"
%tbooltrue
%.2fทศนิยม 2 ตำแหน่ง2500.00
%%เครื่องหมาย % จริง ๆ%

9 · กับดัก Day 1

เขียนคิดว่าเกิดจริง
import "os" แล้วไม่ใช้แค่ warning 💥 compile error "os" imported and not used
ประกาศตัวแปรแล้วไม่ใช้ไม่เป็นไร 💥 declared and not used
count := 0 นอกฟังก์ชันได้เหมือนในฟังก์ชัน 💥 non-declaration statement outside function body
i + f (int + float64)Go แปลงให้ 💥 mismatched types → ต้อง float64(i) + f
a / b เมื่อทั้งคู่เป็น intได้ทศนิยม 5/2 = 2 ตัดเศษทิ้ง → แปลงเป็น float64 ก่อนหาร
int(f) เมื่อ f = 3.9ปัดเป็น 4 3 — ตัดทิ้ง ไม่ปัด
uint8(big) เมื่อ big = 300error หรือ 255 44 — ล้นแล้ววนกลับเงียบ ๆ
int(3.9) ใส่ค่าคงที่ตรง ๆเหมือนผ่านตัวแปร 💥 compile error — constant Go ตรวจได้ตั้งแต่ compile time
len("สวัสดี")ได้ 6 18 — นับ bytes → ใช้ len([]rune(s))