ข้ามไปยังเนื้อหา

Standard Library

หนึ่งในคุณสมบัติที่โดดเด่นของ 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, strconvfmt & strings
วันที่, ระยะเวลา, timertimetime
JSON marshal / unmarshalencoding/jsonencoding/json
Streaming I/O, การสแกนio, bufioio & bufio
ไฟล์, environment, exit codeosใช้ทั่วทั้งโมดูลนี้ (ดู io & bufio) — ไม่มีบทเรียนเฉพาะ
Error value, wrapping, panicerrorsโมดูล Errors & Generics
การยกเลิก, deadline, request-scoped valuecontextConcurrency → context
Structured logginglog/slogไม่ได้สอนเจาะลึกในคอร์สนี้
HTTP server และ clientnet/httpnet/http
Mutex, WaitGroup, OncesyncConcurrency → sync-primitives

โมดูลนี้ตั้งใจไม่ครอบคลุม log/slog เกินกว่าที่กล่าวถึงผ่านๆ — มันใหญ่พอที่จะควรมีการอธิบายของตัวเอง และทุกอย่างที่ต้องรู้เพื่ออ่าน docs ของมัน (interface, struct, error handling, io.Reader/io.Writer) ถูกสอนไว้ที่นี่และใน Errors & Generics แล้ว

โปรแกรม Go จริงๆ จะเรียก fmt.Println, strings.Split, time.Now, และ json.Marshal ตั้งแต่บรรทัดแรกๆ การเข้าใจ package เหล่านี้อย่างถ่องแท้หมายความว่าคุณจะจำ idiomatic Go ได้ในทุก codebase ที่พบ และเลือกใช้เครื่องมือที่ถูกต้องแทนการดึง dependency เพิ่มเติม

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 ใช้ reflectionprint ค่าใดก็ได้ช้ากว่า structured logger ใน production
net/http built-indeploy 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

Package ใดที่ให้บริการ fmt.Sprintf?
Standard library ของ Go ถูกออกแบบมาเพื่อให้โปรแกรมประจำวันส่วนใหญ่ต้องการ:
Package ใดที่ใช้แปลง string "42" เป็น integer 42?
json.Marshal คืนค่าอะไรเมื่อสำเร็จ?