Go ที่ไม่มีใน TypeScript
ทำไม Go บางอย่างถึงไม่มีใน TypeScript
หัวข้อที่มีชื่อว่า “ทำไม Go บางอย่างถึงไม่มีใน TypeScript”TypeScript เป็น layer ที่วางซ้อนบน JavaScript รันบน runtime ที่มี garbage collector (V8 หรือ Deno) และทุก object เป็น reference โดยอัตโนมัติ ส่วน Go ออกแบบมาจากศูนย์ให้เป็นภาษา compiled, statically typed ที่ทำงานระดับ system ได้ รากฐานที่ต่างกันนี้ทำให้ Go มีฟีเจอร์ทั้งหมวดที่ TypeScript ไม่มี ไม่ใช่เพราะ TS ลืม แต่เพราะโมเดลเบื้องล่างต่างกัน
Module นี้ครอบคลุมฟีเจอร์เหล่านั้น เรียนไปทีละบทเพื่อสร้าง intuition กับส่วนของ Go ที่จะรู้สึกแปลกที่สุดในตอนแรก แต่ทรงพลังที่สุดเมื่อเข้าใจแล้ว
สิ่งที่คุณจะเรียนรู้ใน Module นี้
หัวข้อที่มีชื่อว่า “สิ่งที่คุณจะเรียนรู้ใน Module นี้”| บทเรียน | คอนเซปต์ Go-only |
|---|---|
| Pointers | & / * — memory address แบบชัดเจน |
| Value vs Reference | Struct และ array คัดลอก; slice และ map แชร์ข้อมูล |
| Defer / Panic / Recover | cleanup แบบ deterministic และ panic handling |
| Struct Embedding | Composition โดยไม่ต้องใช้ class inheritance |
| Goroutines Preview | Lightweight thread — ไม่ใช่ async/await |
| Static Binaries & Toolchain | Binary เดียว, ไม่ต้องการ runtime, cross-compile ได้เร็ว |
| Generics Differences | Type parameter พร้อม interface constraint — ไม่ใช่ structural |
จุดร่วมของทุกฟีเจอร์
หัวข้อที่มีชื่อว่า “จุดร่วมของทุกฟีเจอร์”ทุกฟีเจอร์มีแรงจูงใจเดียวกัน คือ ความชัดเจนและคาดเดาได้ในราคาถูก Go เลือกทำให้ side-effect มองเห็นได้ (ใครเป็นเจ้าของ memory นี้? function นี้รับ copy หรือแชร์ข้อมูลเดิม?) มากกว่าความสะดวกแบบ implicit ที่ JavaScript/TypeScript ให้ การแลกนี้ทำให้เจอบั๊กแปลก ๆ บน production น้อยลง แต่แลกกับการคิดเผื่อมากขึ้นนิดหน่อยตั้งแต่ต้น
// TypeScript has no pointers, no explicit value-copy semantics,// no defer keyword, no struct embedding, no goroutines,// and no notion of "compile to a static binary".// These are all Go-specific.// Go — a taste of each concept in this modulepackage main
import "fmt"
type Logger struct{ prefix string }func (l Logger) Log(msg string) { fmt.Println(l.prefix+":", msg) }
type Service struct { Logger // embedded — no inheritance needed name string}
func riskyOp() (err error) { defer func() { // defer: always runs on exit if r := recover(); r != nil { err = fmt.Errorf("recovered: %v", r) } }() panic("something went wrong")}
func double[T int | float64](v T) T { return v * 2 } // generics
func main() { svc := Service{Logger: Logger{prefix: "SVC"}, name: "demo"} svc.Log("starting") // promoted method from Logger
x := 10 p := &x // pointer to x *p = 99 // mutate through pointer fmt.Println("x via pointer:", x)
err := riskyOp() fmt.Println("riskyOp err:", err)
fmt.Println("double 7:", double(7))}