mainエントリーポイント
スクリプトがmain関数から始まる理由を学びます
「mainエントリーポイント」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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エントリーポイント」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「mainエントリーポイント」で何を学びますか?
スクリプトがmain関数から始まる理由を学びます ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「mainエントリーポイント」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 初めての.mojoファイル
- mainエントリーポイント
- mojo runで実行する
- Mojo REPLで試す