Standard Library
Standard library แบบ batteries-included ของ Go
หัวข้อที่มีชื่อว่า “Standard library แบบ batteries-included ของ Go”หนึ่งในคุณสมบัติที่โดดเด่นของ Go คือภาษานี้มาพร้อม standard library ที่ครบครัน — ไม่ต้องรัน npm install, pip install, หรือระบุ Maven coordinates สำหรับงานประจำวันส่วนใหญ่ Library ครอบคลุม HTTP server และ client, การเข้ารหัส JSON, cryptography, file I/O, การจัดการข้อความ, concurrency primitives และอีกมากมาย แนวคิดนี้ทำให้ dependency tree สั้นกระชับ binary พึ่งพาตัวเอง และความเสี่ยงในการ upgrade ต่ำ
ตารางด้านล่างแสดงเนื้อหาของโมดูลนี้และ package ที่รับผิดชอบในแต่ละหัวข้อ
| หัวข้อ | Package | บทเรียน |
|---|---|---|
| Formatted I/O, ข้อความ, ตัวเลข | fmt, strings, strconv | fmt & strings |
| วันที่, ระยะเวลา, timer | time | time |
| JSON marshal / unmarshal | encoding/json | encoding/json |
| Streaming I/O, การสแกน | io, bufio | io & bufio |
| ไฟล์, environment, exit code | os | ใช้ทั่วทั้งโมดูลนี้ (ดู io & bufio) — ไม่มีบทเรียนเฉพาะ |
| Error value, wrapping, panic | errors | โมดูล Errors & Generics |
| การยกเลิก, deadline, request-scoped value | context | Concurrency → context |
| Structured logging | log/slog | ไม่ได้สอนเจาะลึกในคอร์สนี้ |
| HTTP server และ client | net/http | net/http |
| Mutex, WaitGroup, Once | sync | Concurrency → sync-primitives |
โมดูลนี้ตั้งใจไม่ครอบคลุม log/slog เกินกว่าที่กล่าวถึงผ่านๆ — มันใหญ่พอที่จะควรมีการอธิบายของตัวเอง และทุกอย่างที่ต้องรู้เพื่ออ่าน docs ของมัน (interface, struct, error handling, io.Reader/io.Writer) ถูกสอนไว้ที่นี่และใน Errors & Generics แล้ว
ทำไมต้องเรียน standard library ก่อน?
หัวข้อที่มีชื่อว่า “ทำไมต้องเรียน standard library ก่อน?”โปรแกรม Go จริงๆ จะเรียก fmt.Println, strings.Split, time.Now, และ json.Marshal ตั้งแต่บรรทัดแรกๆ การเข้าใจ package เหล่านี้อย่างถ่องแท้หมายความว่าคุณจะจำ idiomatic Go ได้ในทุก codebase ที่พบ และเลือกใช้เครื่องมือที่ถูกต้องแทนการดึง dependency เพิ่มเติม
Tour โปรแกรมที่รันได้
หัวข้อที่มีชื่อว่า “Tour โปรแกรมที่รันได้”snippet ด้านล่างครอบคลุมทั้งสี่หัวข้อใน function main เดียว อ่าน output แล้วย้อนติดตาม call แต่ละอันกลับไปยัง package ต้นทาง
package main
import ( "encoding/json" "fmt" "strings" "time")
func main() { // fmt — formatted output fmt.Println("Hello, Go!") fmt.Printf("Pi is approximately %.4f\n", 3.14159) msg := fmt.Sprintf("2 + 2 = %d", 4) fmt.Println(msg)
// strings — text manipulation s := "the quick brown fox" fmt.Println(strings.ToUpper(s)) fmt.Println(strings.Contains(s, "fox")) fmt.Println(strings.Join(strings.Split(s, " "), "-"))
// time — fixed date for deterministic output t := time.Date(2024, 1, 2, 15, 4, 5, 0, time.UTC) fmt.Println(t.Format("2006-01-02"))
// encoding/json — marshal a struct type Point struct { X int `json:"x"` Y int `json:"y"` } p := Point{X: 3, Y: 7} b, _ := json.Marshal(p) fmt.Println(string(b))} ข้อแลกเปลี่ยน
หัวข้อที่มีชื่อว่า “ข้อแลกเปลี่ยน”| สิ่งที่ได้ | ประโยชน์ | ต้นทุน |
|---|---|---|
| batteries-included standard library | ไม่ต้อง install external package สำหรับงานพื้นฐาน | opinionated API — ยืดหยุ่นน้อยกว่า third-party |
| encoding/json (reflect-based) | ไม่ต้อง code generation | ช้ากว่า code-gen approach เช่น easyjson |
| fmt ใช้ reflection | print ค่าใดก็ได้ | ช้ากว่า structured logger ใน production |
| net/http built-in | deploy HTTP server ได้ทันที | net/http รองรับ method + wildcard pattern ตั้งแต่ 1.22 (GET /items/{id}) — third-party router เหลือประโยชน์เรื่อง middleware ecosystem |
ความเข้าใจผิดที่พบบ่อย
หัวข้อที่มีชื่อว่า “ความเข้าใจผิดที่พบบ่อย”- standard library เหมาะสำหรับทุก use case — บางงานต้องการ third-party:
log/slogอยู่ใน stdlib ตั้งแต่ 1.21 อยู่แล้ว แต่ zap/zerolog (structured logging ทางเลือก), chi/gin (advanced routing), gorm (ORM) ยังเป็น third-party - fmt.Println ช้าเกินไปสำหรับ production — fmt เป็นปัญหาเฉพาะเมื่อ log ใน hot path — ควร benchmark ก่อน optimize
- encoding/json คือทางเลือกเดียวสำหรับ Go JSON — มี jsoniter, easyjson, sonic ที่เร็วกว่า 2-5x สำหรับ high-throughput use case
💡 ตัวอย่างจากของจริง
Kubernetes ใช้ standard library เป็นหลัก — net/http สำหรับ API server, encoding/json สำหรับ resource serialization, time สำหรับ lease/timeout
Docker deploy เป็น single binary โดยไม่ต้อง install runtime — เป็นไปได้เพราะ Go compile static binary พร้อม standard library
Prometheus ใช้ net/http สำหรับ /metrics endpoint และ time สำหรับ scrape interval — ไม่ต้องใช้ external HTTP framework