Skip to content

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 ด้านล่าง

บทเรียน

ระดับ: 🟢 พื้นฐาน · 🟡 กลาง · 🔴 ขั้นสูง (ข้ามได้ถ้ายังไม่พร้อม)

#บทระดับสอนอะไร
0Go คืออะไร + Setup🟢ปรัชญา Go, install, first program, tooling
1Variables + Types🟢basic types, var/const, type conversion, zero value
2Control Flow🟢if, for (only loop!), switch, defer
3Functions🟢multiple return, named return, variadic, closure
4Collection (Array, Slice, Map)🟢slice internals, map, range, copy semantics
5Structs + Methods🟢struct, method, pointer vs value receiver, composition
6Interfaces🟡implicit implementation, empty interface, type assertion
7Errors🟡error interface, errors.Is/As, wrapping, panic vs error
8Goroutines + Channels🟡goroutine, channel, select, sync package, context
9Packages + Modules🟢package convention, go.mod, dependency management
10Testing🟡go test, table-driven, benchmarks, mocking
11Standard Library🟡net/http, encoding/json, io, fmt, log/slog
12Modern Go🟡generics, slog, embed, iter, context pattern, configuration, DI
12bProduction Playbook🔴graceful shutdown, container, observability, profiling, performance, security
13Web Framework Deep🔴Gin, Echo, Fiber, Chi, middleware, validation, graceful shutdown, OTel
14Database in Go🔴database/sql, pgx, sqlc, GORM, Ent, migrations, connection pool, Testcontainers
15gRPC + Protobuf🔴proto definition, server/client, streaming, interceptors, mTLS, deadline, gRPC Gateway, schema evolution

Feature → Minimum Go Version

ตัวอย่างในเล่มใช้ Go 1.22+ เป็น baseline — ตารางนี้ระบุชัดว่าฟีเจอร์ไหนต้องเวอร์ชันไหน:

ฟีเจอร์เวอร์ชันต่ำสุดหมายเหตุ
embed1.16ฝังไฟล์ใน binary
any alias, generics1.18type parameter [T any]
Fuzz testing1.18go test -fuzz
errors.Join1.20รวมหลาย error
rand.Seed deprecated1.20global generator auto-seeded (ไม่ต้องเรียกแล้ว — Go ตั้ง seed ให้อัตโนมัติ)
slices / maps / cmp package1.21generic helper ใน stdlib
log/slog1.21structured logging
Method matching ใน ServeMux (GET /path)1.22path value r.PathValue(...)
Loop var per-iteration scope1.22แก้ closure capture bug
math/rand/v21.22API ใหม่, 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 package1.23lazy 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 errorerror ค่าคงที่ที่ประกาศไว้ล่วงหน้า (เช่น ErrNotFound)
contextobject พา 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 schedulerM goroutine map ลงบน N OS threadบท 8 (อ่านผ่านได้รอบแรก)
structured logginglog เป็นคู่ 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
distrolesscontainer image ที่ตัด OS/shell ออกหมด เหลือแค่ binaryบท 12b
mTLSTLS สองทาง — server ตรวจ client cert ด้วยบท 12b
head-of-line blockingrequest แรกในคิวบล็อก request ถัดไปทั้งหมดบท 13/15
fasthttpHTTP 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
CORSCross-Origin Resource Sharing — กลไกที่ browser ใช้คุมว่าเว็บ A เรียก API ของเว็บ B ได้ไหมบท 13
CVECommon Vulnerabilities and Exposures — รหัสมาตรฐานของช่องโหว่ security ที่ประกาศแล้วบท 12b

วิธีอ่านที่แนะนำ

  1. เขียน Go ทุกบท — ใช้ playground (https://go.dev/play) ก็ได้
  2. Read Go source code — standard library เขียน Go ที่เป็นตัวอย่างดี
  3. ทำตามสไตล์ Go — gofmt + golint + go vet
  4. อย่าเอา pattern Java มาใช้ — Go มี style ของตัวเอง

เรื่อง Checkpoint (โจทย์ท้ายบท)

โจทย์ในแต่ละบทออกแบบให้ "ลองเขียนเอง" — ตั้งใจไม่ให้เฉลย เพราะการแก้เองคือจุดที่ Go กลายเป็น "ภาษาของเรา" ไม่ใช่ภาษาที่ "อ่านผ่านตา" ติดตรงไหนให้ทำดังนี้:

  1. ลองคิด/เขียนเองอย่างน้อย 30 นาทีก่อน — ติดให้ดูดี (พยายามก่อนค่อยขอความช่วยเหลือ)
  2. เปิด Go by Example — มีตัวอย่างเทียบเคียงเกือบทุก checkpoint
  3. ค้น pkg.go.dev สำหรับ API ของ stdlib
  4. ถามชุมชน — r/golang, Gopher Slack

ถ้า stuck เกิน 1 ชั่วโมง — ข้ามไปก่อนได้ กลับมาเมื่อรู้มากขึ้น สำคัญที่ทำเองอย่าลอก


← กลับสารบัญหลัก


🔤 Glossary · 📋 Style guide · 📅 last_verified: 2026-06-12