Hello, World!
โปรแกรม Go แรกของคุณ
หัวข้อที่มีชื่อว่า “โปรแกรม Go แรกของคุณ”ใน TypeScript/Node.js คุณสร้างไฟล์, export หรือเขียนโค้ดไว้ที่ top-level แล้ว node ก็รันให้ ส่วน Go กำหนดให้ทุกโปรแกรมที่รันได้ต้องมีโครงสร้างเฉพาะ: ประกาศ package main และมี entry point func main() มาเทียบกัน
// hello.tsconsole.log("Hello, World!");
// run with: npx ts-node hello.ts// or compile: tsc hello.ts && node hello.js// hello.gopackage main
import "fmt"
func main() { fmt.Println("Hello, World!")}
// run with: go run hello.goอธิบายทีละบรรทัด
หัวข้อที่มีชื่อว่า “อธิบายทีละบรรทัด”package main
ทุกไฟล์ Go เริ่มด้วยการประกาศ package และ package ชื่อ main นั้นพิเศษ — เป็นตัวบอกว่านี่คือโปรแกรมที่รันได้ ไม่ใช่ library เทียบได้กับไฟล์ที่ field "main" ใน package.json ชี้ไปหา
import "fmt"
การ import ของ Go เป็นแบบ explicit และทำทีละไฟล์ fmt คือ standard library package สำหรับงาน formatted I/O — เทียบได้กับ console ในโลก Go คุณ import ด้วย full package path แบบ string (ต้องใส่ quote) และ import ที่ไม่ได้ใช้จะกลายเป็น compile error
func main()
Function main คือ entry point ทุกโปรแกรม Go ที่รันได้ต้องมี func main() เพียงตัวเดียวใน package main และไม่รับ argument ใด ๆ — ถ้าต้องการ command-line argument ให้อ่านจาก os.Args
// TypeScript: multiple entry styles// 1. Top-level code (scripts)console.log("runs immediately");
// 2. "main" in package.json points here// 3. Exported function called by framework// Go: always one entry pointpackage main
import "fmt"
func main() { // Program starts here, always. fmt.Println("Runs first.")}fmt.Println vs console.log
หัวข้อที่มีชื่อว่า “fmt.Println vs console.log”fmt.Println คือสิ่งที่ใกล้เคียง console.log ที่สุด นอกจากนี้ package fmt ยังมี fmt.Printf สำหรับ format string (เหมือน printf ใน C หรือ template literal ใน JS) และ fmt.Sprintf สำหรับสร้าง string โดยไม่ print ออกมา
// TypeScript loggingconsole.log("Hello"); // simpleconsole.log("Name:", name); // multiple argsconsole.log("Score: " + score); // string concatenationconst msg = "Hello, " + name + "!"; // build a string// Go fmt packagefmt.Println("Hello") // simplefmt.Println("Name:", name) // multiple argsfmt.Printf("Score: %d\n", score) // format verbmsg := fmt.Sprintf("Hello, %s!", name) // build a stringFormat verb
หัวข้อที่มีชื่อว่า “Format verb”fmt.Printf ใช้ format verb แทน template literal ตัวที่ใช้บ่อยมีดังนี้:
| Verb | ความหมาย | เทียบเท่า TypeScript |
|---|---|---|
%s | string | ${str} |
%d | integer | ${num} |
%f | float | ${num.toFixed(6)} |
%.2f | float, ทศนิยม 2 ตำแหน่ง | ${num.toFixed(2)} |
%v | ค่าใดๆ (รูปแบบ default) | ${JSON.stringify(val)} |
%T | ชื่อ type | typeof val |
%+v | struct พร้อมชื่อ field | JSON.stringify(val, null, 2) |
ลองเลย — รันโปรแกรม Go แรกของคุณ
หัวข้อที่มีชื่อว่า “ลองเลย — รันโปรแกรม Go แรกของคุณ”package main
import "fmt"
func main() { fmt.Println("Hello, World!")
// fmt.Printf uses format verbs — like template literals name := "Gopher" age := 3 fmt.Printf("Name: %s, Age: %d\n", name, age)
// fmt.Sprintf builds a string without printing greeting := fmt.Sprintf("Welcome, %s!", name) fmt.Println(greeting)}Compiling on go.dev (Go 1.27)… offline? an in-browser runtime (~8 MB, Go 1.26) is used instead