Der main-Einstiegspunkt
Warum Skripte bei der main-Funktion beginnen.
Der main-Einstiegspunkt ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎯
Häufig gestellte Fragen
Ist die Lektion „Der main-Einstiegspunkt“ kostenlos?
Ja — der vollständige Text von „Der main-Einstiegspunkt“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Der main-Einstiegspunkt“?
Warum Skripte bei der main-Funktion beginnen. Du übst Mojo Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Mojo Academy zu starten?
Keine Vorkenntnisse erforderlich. Mojo Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Der main-Einstiegspunkt“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Mojo Academy-Lektion Code schreiben und ausführen?
Ja. Jede Mojo Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Ihre erste .mojo-Datei
- Der main-Einstiegspunkt
- Mit mojo run ausführen
- Im Mojo-REPL experimentieren