โหมดมืด
Go
ส่วนหนึ่งของ Beginner Book
หนังสือเล่มนี้พาคุณไปถึงไหน
อ่านจบ + ทำ checkpoint หมด คุณจะ:
- เข้าใจปรัชญาของ Go — "less is more"
- เขียน Go ได้จริง (CLI tool, web server, microservice)
- ใช้ goroutines (หน่วยทำงานพร้อมกัน) + channels (ท่อส่งข้อมูลระหว่างกัน) — concurrency ที่ Go ออกแบบให้ในตัว
- เข้าใจ Go modules, testing, error handling แบบ idiomatic
- พร้อมขึ้น framework (ชุดเครื่องมือสำเร็จรูปสำหรับสร้างเว็บ/API เช่น Gin, Echo, Fiber) หรือ tool ใหญ่ ๆ ในวงการ (Kubernetes, Docker, Terraform — ทั้งหมดเขียนด้วย Go)
หนังสือนี้ออกแบบมาให้ใคร
✅ คนที่รู้ Java/Python/JS อยู่แล้ว — Go จะเรียนเร็ว
✅ Backend developer ที่อยากเขียนเครื่องมือ CLI / งาน DevOps / microservices
✅ คนที่อยากเข้าใจ Kubernetes, Docker source code
❌ Programmer มืออาชีพที่ใช้ Go อยู่แล้ว 5+ ปี
📖 ระดับของเล่มนี้: บท 0–8 เข้าถึงได้หากรู้ตัวแปร, loop, function จากภาษาใดภาษาหนึ่งแล้ว — บท 9–15 ต้องการพื้นฐาน programming มากขึ้นและข้ามได้ถ้ายังไม่พร้อม
⚠️ ถ้านี่คือครั้งแรกที่เขียนโปรแกรมในชีวิต บท 12–15 (Modern Go, Production, Web Framework, Database, gRPC) ถือว่าคุณใช้ pointer, interface, และ concurrency จากบท 0–8 ได้คล่องมือแล้ว — อย่าเพิ่งแตะจนกว่าจะเขียนโปรแกรมเล็ก ๆ ที่รันได้จริงด้วยตัวเองสักชิ้นก่อน
🌱 ถ้ายังไม่เคยเขียนโปรแกรมเลย แนะนำเริ่มที่ JavaScript บท 0 ก่อน แล้วค่อยกลับมา Go จะเข้าใจง่ายขึ้นมาก
ทำไม Go
text
ปรัชญาของ Go (Google, 2009):
- Simple > clever
- Compile fast
- Run fast (startup เร็วกว่า Java; runtime speed ใกล้เคียง JVM สำหรับ I/O-bound workload — JVM อาจเร็วกว่าบน CPU-intensive ที่รันนานเพราะมี JIT profiling)
- Static typed
- Built-in concurrency (goroutines)
- Single binary (no JVM, no node_modules)
- Garbage collected
- ไม่มี OOP แบบเก่า (no inheritance) — composition only (ประกอบจาก struct เล็ก ๆ แทน — เหมือนต่อ Lego แทนสร้างต้นไม้ class)เปรียบเทียบ
text
ภาษา Compile Runtime Memory Concurrency Verbosity
(ความเร็ว (ความเร็ว (ใช้แรม (รันงานคู่ขนาน (ความเยิ่นเย้อ
ตอนแปล) ตอนรัน) เท่าไหร่) ดีแค่ไหน) ของโค้ด)
──────────────────────────────────────────────────────────────────────────
Go Fast Fast Low Native ⭐ Low
Java Medium Fast High Library Medium
Python None Slow Medium GIL ⚠️ Very Low
Node.js None Medium Medium Event loop Low
Rust Slow Fastest Lowest Library Highคำในตารางด้านบน:
- Native ⭐ = ภาษาออกแบบมาให้ทำงานขนานในตัว (goroutine)
- Library = ต้องใช้ library เสริมถึงจะทำงานขนานได้
- Event loop = วิธีของ Node ที่รันงาน I/O สลับกันในเธรดเดียว
- GIL (Global Interpreter Lock = กุญแจล็อก interpreter ระดับ global) = กุญแจของ Python ที่ทำให้รัน thread ขนานจริงไม่ได้
→ Go = "Pragmatic productivity" (เน้นเขียนได้จริง ใช้งานได้จริง) — เลือกเมื่อต้องการความเร็ว/ประสิทธิภาพ (performance) + DX (developer experience = ประสบการณ์ใช้งานของนักพัฒนา) + deploy ง่าย
เครื่องมือที่ใช้
- Go 1.22+ (generics มีมาตั้งแต่ 1.18; 1.22 เพิ่ม method matching ใน ServeMux, ranges-over-int, loop var per-iteration scope)
- VS Code + Go extension หรือ GoLand
- delve debugger (ตัว debug โปรแกรม Go — ใช้ดูค่าตัวแปร, หยุดทีละบรรทัด, ตามรอย call stack) — ติดตั้ง:
go install github.com/go-delve/delve/cmd/dlv@latest - go test built-in testing (ระบบทดสอบที่มากับภาษา ไม่ต้องลง library เพิ่ม)
📌 ตัวอย่างในเล่มต้องใช้ Go 1.22+ — ดูรายละเอียดว่าฟีเจอร์ไหนต้องเวอร์ชันไหนที่หัวข้อ Feature → Minimum Go Version ด้านล่าง
บทเรียน
ระดับ: 🟢 พื้นฐาน · 🟡 กลาง · 🔴 ขั้นสูง (ข้ามได้ถ้ายังไม่พร้อม)
| # | บท | ระดับ | สอนอะไร |
|---|---|---|---|
| 0 | Go คืออะไร + Setup | 🟢 | ปรัชญา Go, install, first program, tooling |
| 1 | Variables + Types | 🟢 | basic types, var/const, type conversion, zero value |
| 2 | Control Flow | 🟢 | if, for (only loop!), switch, defer |
| 3 | Functions | 🟢 | multiple return, named return, variadic, closure |
| 4 | Collection (Array, Slice, Map) | 🟢 | slice internals, map, range, copy semantics |
| 5 | Structs + Methods | 🟢 | struct, method, pointer vs value receiver, composition |
| 6 | Interfaces | 🟡 | implicit implementation, empty interface, type assertion |
| 7 | Errors | 🟡 | error interface, errors.Is/As, wrapping, panic vs error |
| 8 | Goroutines + Channels | 🟡 | goroutine, channel, select, sync package, context |
| 9 | Packages + Modules | 🟢 | package convention, go.mod, dependency management |
| 10 | Testing | 🟡 | go test, table-driven, benchmarks, mocking |
| 11 | Standard Library | 🟡 | net/http, encoding/json, io, fmt, log/slog |
| 12 | Modern Go | 🟡 | generics, slog, embed, iter, context pattern, configuration, DI |
| 12b | Production Playbook | 🔴 | graceful shutdown, container, observability, profiling, performance, security |
| 13 | Web Framework Deep | 🔴 | Gin, Echo, Fiber, Chi, middleware, validation, graceful shutdown, OTel |
| 14 | Database in Go | 🔴 | database/sql, pgx, sqlc, GORM, Ent, migrations, connection pool, Testcontainers |
| 15 | gRPC + Protobuf | 🔴 | proto definition, server/client, streaming, interceptors, mTLS, deadline, gRPC Gateway, schema evolution |
Feature → Minimum Go Version
ตัวอย่างในเล่มใช้ Go 1.22+ เป็น baseline — ตารางนี้ระบุชัดว่าฟีเจอร์ไหนต้องเวอร์ชันไหน:
| ฟีเจอร์ | เวอร์ชันต่ำสุด | หมายเหตุ |
|---|---|---|
embed | 1.16 | ฝังไฟล์ใน binary |
any alias, generics | 1.18 | type parameter [T any] |
| Fuzz testing | 1.18 | go test -fuzz |
errors.Join | 1.20 | รวมหลาย error |
rand.Seed deprecated | 1.20 | global generator auto-seeded (ไม่ต้องเรียกแล้ว — Go ตั้ง seed ให้อัตโนมัติ) |
slices / maps / cmp package | 1.21 | generic helper ใน stdlib |
log/slog | 1.21 | structured logging |
Method matching ใน ServeMux (GET /path) | 1.22 | path value r.PathValue(...) |
| Loop var per-iteration scope | 1.22 | แก้ closure capture bug |
math/rand/v2 | 1.22 | API ใหม่, auto-seeded, algorithm ดีกว่า (PCG/ChaCha8) — แต่ยังเป็น PRNG ไม่ใช่ crypto-secure; ใช้ crypto/rand สำหรับ token/password |
Range over int (for i := range 10) | 1.22 | |
Range over function / iter package | 1.23 | lazy iterator |
Glossary — ศัพท์อังกฤษที่เจอบ่อย
ในเล่มจะกางความหมายไทยตอนใช้ครั้งแรก — แต่ถ้าลืมหรืออ่านข้ามไป เปิดที่นี่ได้
💡 แบ่งเป็น 2 กลุ่ม เพื่อลด overload สำหรับมือใหม่ — กลุ่มแรก "พื้นฐาน" เจอตั้งแต่บทต้น ๆ ส่วน "ขั้นสูง" ค่อยอ่านเมื่อถึงบท 12+ (Production / Modern Go / Web Framework Deep)
ศัพท์พื้นฐาน (เจอบทต้น ๆ)
| คำ | ความหมายไทยสั้น |
|---|---|
| goroutine | หน่วยทำงานพร้อมกันแบบเบาของ Go (KB stack) |
| channel | ท่อส่งข้อมูลระหว่าง goroutine |
| closure | ฟังก์ชันที่จำตัวแปรนอก scope ของตัวเองได้ |
| receiver | ตัวแปรหน้าชื่อ method ที่เป็น "this/self" |
| idiomatic | สำนวนที่ชาว Go ถือว่าเขียนถูกสไตล์ |
| refactor | ปรับโครงสร้างโค้ดภายในให้ดีขึ้นโดยไม่เปลี่ยนพฤติกรรมภายนอก |
| exported / unexported | ขึ้นต้นพิมพ์ใหญ่ = นอก package เห็นได้ / พิมพ์เล็ก = เห็นเฉพาะใน package |
| wrap (error) | ห่อ error เพื่อเก็บ context — ใช้ %w ใน fmt.Errorf |
| sentinel error | error ค่าคงที่ที่ประกาศไว้ล่วงหน้า (เช่น ErrNotFound) |
| context | object พา deadline/cancel/value ข้าม goroutine |
| deadline / timeout | เวลาที่จำกัดให้งานเสร็จ |
| race condition | หลาย goroutine แตะข้อมูลเดียวกันพร้อมกันจนผลเพี้ยน |
| deadlock | ทุก goroutine รอกันจนค้างทั้งระบบ |
| leak (goroutine) | goroutine ที่ไม่จบ — กิน memory ไปเรื่อย |
| middleware | ฟังก์ชันคั่นกลางก่อน/หลัง handler |
| handler | ฟังก์ชันตอบ HTTP request |
| benchmark | การวัดความเร็ว/ประสิทธิภาพของโค้ดโดยรันซ้ำหลายพัน/ล้านครั้งแล้วเก็บค่าเฉลี่ย |
| anti-pattern | แพตเทิร์นที่ "ดูดี" แต่จริง ๆ เป็นกับดัก |
| cargo-cult | ทำตามโดยไม่เข้าใจว่าทำไปทำไม |
ศัพท์ขั้นสูง (อ่านเมื่อถึงบท 12+ / Production)
| คำ | ความหมายไทยสั้น | บทที่ใช้ |
|---|---|---|
| M:N scheduler | M goroutine map ลงบน N OS thread | บท 8 (อ่านผ่านได้รอบแรก) |
| structured logging | log เป็นคู่ key-value / JSON แทนข้อความเปล่า | บท 12 |
| observability | ความสามารถมองเห็นสถานะระบบ (logs + metrics + traces) | บท 12 |
| metrics | ตัวเลขชี้วัดระบบ (Prometheus) | บท 12 |
| tracing | เส้นทาง request ข้ามบริการ (OpenTelemetry) | บท 12 |
| cardinality | จำนวน combination ของ label ใน metric — ถ้าระเบิดจะกิน memory เซิร์ฟเวอร์ | บท 12 |
| graceful shutdown | ปิด server โดยรอ request ที่ค้างจบก่อน | บท 12 |
| distroless | container image ที่ตัด OS/shell ออกหมด เหลือแค่ binary | บท 12b |
| mTLS | TLS สองทาง — server ตรวจ client cert ด้วย | บท 12b |
| head-of-line blocking | request แรกในคิวบล็อก request ถัดไปทั้งหมด | บท 13/15 |
| fasthttp | HTTP library ของ Go ที่ไม่ใช้ net/http (เร็วกว่าแต่ไม่ compatible) | บท 13 |
| radix / trie tree | โครงสร้างข้อมูลที่ใช้ใน fast router ของ Gin/Chi | บท 13 |
| escape analysis | การวิเคราะห์ว่าตัวแปรต้องไป heap หรืออยู่ stack ได้ | บท 12 |
| GC pressure | งานหนักที่ garbage collector ต้องเก็บกวาด — มาก = ช้า | บท 12 |
| payload | ข้อมูลจริงที่ส่งใน request/response (body ของ HTTP, ข้อความใน message) | บท 13/14 |
| service mesh | ชั้นเครือข่ายระหว่าง microservice (เช่น Istio, Linkerd) — จัดการ traffic/security นอกตัวแอป | บท 12b |
| N+1 problem | ดึงข้อมูล 1 query แล้วยิงอีก N query ตามมาทีละแถว — ช้ามากใน DB | บท 14 |
| CORS | Cross-Origin Resource Sharing — กลไกที่ browser ใช้คุมว่าเว็บ A เรียก API ของเว็บ B ได้ไหม | บท 13 |
| CVE | Common Vulnerabilities and Exposures — รหัสมาตรฐานของช่องโหว่ security ที่ประกาศแล้ว | บท 12b |
วิธีอ่านที่แนะนำ
- เขียน Go ทุกบท — ใช้ playground (https://go.dev/play) ก็ได้
- Read Go source code — standard library เขียน Go ที่เป็นตัวอย่างดี
- ทำตามสไตล์ Go — gofmt + golint + go vet
- อย่าเอา pattern Java มาใช้ — Go มี style ของตัวเอง
เรื่อง Checkpoint (โจทย์ท้ายบท)
โจทย์ในแต่ละบทออกแบบให้ "ลองเขียนเอง" — ตั้งใจไม่ให้เฉลย เพราะการแก้เองคือจุดที่ Go กลายเป็น "ภาษาของเรา" ไม่ใช่ภาษาที่ "อ่านผ่านตา" ติดตรงไหนให้ทำดังนี้:
- ลองคิด/เขียนเองอย่างน้อย 30 นาทีก่อน — ติดให้ดูดี (พยายามก่อนค่อยขอความช่วยเหลือ)
- เปิด Go by Example — มีตัวอย่างเทียบเคียงเกือบทุก checkpoint
- ค้น pkg.go.dev สำหรับ API ของ stdlib
- ถามชุมชน — r/golang, Gopher Slack
ถ้า stuck เกิน 1 ชั่วโมง — ข้ามไปก่อนได้ กลับมาเมื่อรู้มากขึ้น สำคัญที่ทำเองอย่าลอก
🔤 Glossary · 📋 Style guide · 📅 last_verified: 2026-06-12