พื้นฐาน Go
Hello World in Go: Structure of a Program
Write your first Go program and see what each part does: package main, import and func main.
สิ่งที่คุณจะได้เรียนรู้
- package main and func main
- Importing fmt
- Printing with fmt.Println
From source to output
- 1
main.goYou write the code - 2
go runGo compiles and starts it - 3
main()The program starts in main - 4
Hello, Go!The text appears
How a Go program runs
Every Go file starts with a package name. A program that you can run lives in package main and starts in the function main. import brings in other packages, such as fmt for printing.
fmt.Println prints its arguments and ends the line. Names that start with a capital letter, such as Println, are the ones a package makes available to other packages.
ประเด็นสำคัญ
- A runnable program lives in package main.
- It starts in func main.
fmt.Printlnprints a line of text.
ตัวอย่าง Go ที่รันได้
main.go
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
Hello, Go!ฝึกฝน
เปิดตัวอย่างในคอมไพเลอร์ออนไลน์ ลองเปลี่ยนค่าหรือเพิ่มบรรทัด แล้วทายผลลัพธ์ก่อนกดรัน จากนั้นลองทำให้โค้ดผิดโดยตั้งใจ แล้วอ่านข้อความจากคอมไพเลอร์
เรียนบทนี้จบแล้วใช่ไหม
ทำเครื่องหมายว่าเรียนแล้วเพื่อติดตามความคืบหน้า
ความคืบหน้าของคุณถูกบันทึกไว้ในเบราว์เซอร์นี้ เข้าสู่ระบบเพื่อเก็บไว้ใช้ได้ทุกอุปกรณ์
ฝึกด้วยโจทย์
ใช้สิ่งที่เรียนในบทนี้แก้ปัญหาจริง ทุกโจทย์รันในเบราว์เซอร์ พร้อมการทดสอบทันทีและรีวิวจาก AI
- คี่, คู่ หรือไม่ใช่ทั้งสองอย่าง จัดประเภทตัวเลขว่าเป็นจำนวนเต็มคี่ จำนวนเต็มคู่ หรือไม่ใช่ทั้งสองอย่าง ง่าย
- ยกกำลังสองตัวเลข คูณจำนวนเต็มกับตัวมันเอง ง่าย