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

Advanced Go — ภาพรวม

ถ้าคุณมาจาก TypeScript คุณคิดเป็น type, จัดการ async flow และ compose พฤติกรรมผ่าน interface เป็นอยู่แล้ว โมดูล Go-101 ปูพื้นฐานไปแล้ว ส่วนโมดูลนี้ครอบคลุมสิ่งที่ยกระดับโค้ดให้พร้อมขึ้น production: อัลกอริทึม generic ที่ type-safe, reflection ที่ runtime สำหรับโค้ดระดับ framework, การเขียน test และ benchmark แบบมีวินัย, การจัดการ module เชิงลึก และ performance profiling

ไม่มีหัวข้อไหนต้องใช้ library พิเศษเลย ทุกอย่างอยู่ใน standard library ของ Go หรือใน toolchain go เอง

บทเรียนสิ่งที่จะได้เรียนเทียบกับ TS
GenericsType parameters, constraint interfaces, comparable, เมื่อใดใช้ generics แทน interfaceTS generics (เปรียบเทียบเชิงลึก)
Reflectionreflect package, struct tags ที่ runtime, รูปแบบที่ปลอดภัยreflect-metadata / decorators
Table-driven teststesting.T, subtests ด้วย t.Run, ตาราง fixture ร่วมกันjest it.each / describe.each
Benchmarkstesting.B, go test -bench, intuition การ profilingbenchmark.js, console.time
Modules เชิงลึกSemantic versioning, go.mod/go.sum, replace, vendoring, workspacesnpm + package-lock.json + npm workspaces
pprof profilingCPU/heap profiles, net/http/pprof, อ่าน flame graphsnode --prof, clinic.js

toolchain ของ Go ตั้งใจออกแบบมาให้มีทุกอย่างพร้อมในตัว คุณไม่ต้องไปหา benchmark runner ภายนอก, profiler plugin หรือ test framework จากเจ้าอื่น คำสั่ง go test ตัวเดียวรันได้ทั้ง unit test, integration test, benchmark และ fuzz test นี่คือการตัดสินใจออกแบบที่ตั้งใจ: workflow เดียว คำสั่งเดียว ไม่ผูกติดกับ framework ไหน

TypeScript
// TypeScript ต้องใช้เครื่องมือแยกกัน:
import { describe, it, expect } from 'vitest';
import Benchmark from 'benchmark';
import { Profile } from 'clinic';
Go
// Go: คำสั่งเดียวจัดการทุกอย่าง
// go test ./... — รัน tests
// go test -bench=. — รัน benchmarks
// go test -fuzz=Fuzz — รัน fuzz tests
// go tool pprof cpu.out — เปิด profiler
คำสั่ง Go ใดที่รัน unit tests และ benchmarks ได้พร้อมกัน?
Generics ถูกเพิ่มเข้า Go ในเวอร์ชันใด?
package ใดให้ runtime reflection ใน Go?