0Pricing
Mojo Academy · บทเรียน

จุดเริ่มต้น main

เหตุใดสคริปต์จึงเริ่มทำงานที่ฟังก์ชัน main

จุดเริ่มต้น main เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Mojo Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Every Program Needs a Start

When you run a Mojo file, the program needs to know where to begin. That starting point is a special function named main. 🚀

Mojo Looks for main

The toolchain searches your file for a function called main and calls it for you. You never call main yourself.

def main():
    print("Program started")

The Entry Point Idea

An entry point is simply where execution enters your code. In Mojo that door is always main, so your logic goes inside it.

Code Outside main

Top-level definitions like functions and structs can live outside main, but the work that actually runs starts from main.

fn greet():
    print("Hi")

def main():
    greet()

main Takes No Arguments

A basic main needs no parameters. The empty parentheses after the name show it accepts nothing when Mojo calls it.

def main():
    print("No args needed")

One main Per Program

A program has exactly one main. Two would confuse Mojo about where to begin, so the compiler expects a single clear entry point.

def or fn for main

You can define main with def or fn. Both work as the entry point; def is the friendlier, Python-like choice when starting out.

fn main():
    print("Strict style")

Order Inside main

Statements inside main run top to bottom, in order. The first line executes first, then the next, building your program step by step.

def main():
    print("first")
    print("second")

Calling Helpers from main

main is often short: it just calls other functions. This keeps your entry point a clean summary of what the program does.

def main():
    setup()
    run()

Forgetting main

If a file has no main, running it as a program fails because Mojo cannot find where to start. Always include one for runnable files.

main Ends the Program

When the last line of main finishes, the program is done and control returns to your shell. No extra cleanup is required.

Quick Check

You wrote a Mojo file with several functions. Where does execution actually begin?

Recap

Every runnable Mojo file needs one main function. Mojo calls it to start, runs its lines in order, and ends when main finishes. 🎯

คำถามที่พบบ่อย

บทเรียน “จุดเริ่มต้น main” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “จุดเริ่มต้น main” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Mojo Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “จุดเริ่มต้น main”

เหตุใดสคริปต์จึงเริ่มทำงานที่ฟังก์ชัน main คุณปฏิบัติ Mojo Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “จุดเริ่มต้น main” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม

ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ไฟล์ .mojo ไฟล์แรกของคุณ
  2. จุดเริ่มต้น main
  3. การเรียกใช้ด้วย mojo run
  4. การสำรวจใน Mojo REPL
← กลับไปที่ Mojo Academy