พื้นฐานและ Syntax
ปรัชญาการออกแบบของ Go
หัวข้อที่มีชื่อว่า “ปรัชญาการออกแบบของ Go”Go ถูกออกแบบโดยทีมที่ Google เพื่อแก้ปัญหาจริงในการพัฒนา software ขนาดใหญ่ ปรัชญาหลักมีสี่ข้อ:
ความเรียบง่าย (Simplicity) — Go เลือกความเรียบง่ายเหนือความหลากหลายของ feature ทุก keyword และ syntax มีเหตุผลที่ชัดเจน ภาษามี keyword เพียง 25 คำ ทำให้เรียนรู้ได้เร็วและอ่าน code ของคนอื่นได้ง่าย
เวลา compile เร็ว (Fast Compile) — Go compile เป็นวินาที ไม่ใช่นาที แม้สำหรับ codebase ขนาดใหญ่ ทำให้ feedback loop สั้นและ iteration เร็ว
วิธีเดียวที่ชัดเจน (One Obvious Way) — สำหรับปัญหาส่วนใหญ่ มักมีวิธีหนึ่งที่ Go แนะนำ ทำให้ codebase ขนาดใหญ่อ่านสม่ำเสมอ เพราะทุกคนเขียนด้วยรูปแบบเดียวกัน
Explicit มากกว่า Implicit — Go บังคับให้คุณระบุประเภทและการแปลงอย่างชัดเจน ไม่มี magic type coercion ที่ซ่อนอยู่
โมดูลนี้ครอบคลุมอะไร
หัวข้อที่มีชื่อว่า “โมดูลนี้ครอบคลุมอะไร”โมดูล Basics จะพาคุณผ่านรากฐานของภาษา:
- Packages และ Imports — วิธีจัดการ code ใน package และ import dependency
- Variables และ Types — การประกาศตัวแปร, zero values, types พื้นฐาน, constants, และ
iota - Control Flow —
if,for(loop keyword เดียวของ Go),switch, และrange - Functions — multiple return values,
defer, closures, และ first-class functions
โปรแกรมแรก
หัวข้อที่มีชื่อว่า “โปรแกรมแรก”ด้านล่างคือโปรแกรม Go ที่แสดงให้เห็น syntax พื้นฐาน: การประกาศตัวแปรด้วย :=, types หลายชนิด, และการ print ผลลัพธ์
package main
import "fmt"
func main() { name := "Go" version := 1.22 fast := true fmt.Println("Hello from", name) fmt.Println("Version:", version) fmt.Println("Fast compile:", fast)}รันด้านล่างเพื่อดู output ทุก concept ที่ใช้ที่นี่จะอธิบายอย่างละเอียดในบทเรียนต่อไป:
package main
import "fmt"
func main() { name := "Go" version := 1.22 fast := true fmt.Println("Hello from", name) fmt.Println("Version:", version) fmt.Println("Fast compile:", fast)
// Integer arithmetic is exact; no implicit float promotion a := 10 b := 3 fmt.Println("10 / 3 =", a/b) fmt.Println("10 % 3 =", a%b)}Loading Go runtime (first run only, ~8 MB)…
ข้อแลกเปลี่ยน
หัวข้อที่มีชื่อว่า “ข้อแลกเปลี่ยน”| สิ่งที่ได้ | ประโยชน์ | ต้นทุน |
|---|---|---|
| static typing | compiler จับ bug ก่อน runtime | เขียน code มากกว่า dynamic language เล็กน้อย |
| keyword เพียง 25 คำ | เรียนรู้เร็ว, อ่าน code คนอื่นได้ง่าย | feature บางอย่าง (เช่น generics ยุคแรก) ต้องใช้ workaround |
| fast compile | feedback loop สั้น, iteration เร็ว | ไม่มี REPL ที่สมบูรณ์แบบ |
| garbage collection | ไม่ต้องจัดการ memory เอง | GC pause ที่ unpredictable ใน latency-critical system |
ความเข้าใจผิดที่พบบ่อย
หัวข้อที่มีชื่อว่า “ความเข้าใจผิดที่พบบ่อย”- “Go คือ C เวอร์ชันใหม่” — Go มี GC, goroutine, interface, และ module system ที่แตกต่างจาก C โดยสิ้นเชิง
- “Go ไม่มี OOP” — Go มี struct, method, และ interface ที่ทำ composition ได้ แต่ไม่มี class hierarchy แบบ Java
- “Go ใช้ได้แค่ backend server” — Go ใช้งานได้กับ CLI tools, DevOps tooling, WebAssembly, embedded systems, และ data pipelines
💡 ตัวอย่างจากของจริง
Kubernetes — เขียนด้วย Go ทั้งหมด จัดการ container หลายล้าน pod บน cloud ทั่วโลก
Docker — runtime และ CLI เขียนด้วย Go ใช้ goroutine ในการจัดการ container lifecycle
Cloudflare — ใช้ Go ใน edge infrastructure เพื่อ handle request หลายสิบล้านครั้งต่อวินาที