使用 mojo run 运行
从命令行执行程序
使用 mojo run 运行 是 CoddyKit 上的免费 Mojo Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Mojo Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Mojo Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
From File to Running Program
You have a saved hello.mojo file. To see it work, you launch it from your terminal with the mojo command. 🖥️
The mojo run Command
The simplest way to execute a file is mojo run followed by the file name. Mojo compiles and runs it in one step.
mojo run hello.mojoRun from the Right Folder
Open your terminal in the folder that holds the file, or give the full path. Otherwise Mojo cannot find hello.mojo to run.
cd my_project
mojo run hello.mojoSee Your Output
After you press enter, Mojo prints the program's output right in the terminal. Your greeting appears on the next line.
Hello, world!The Shorter Form
You can often drop the word run and just pass the file: mojo hello.mojo does the same thing as mojo run.
mojo hello.mojoEdit, Save, Run Again
Change the code, save, and run the command again to see new output. This quick loop is how you learn fast in Mojo.
Reading an Error Message
If you have a typo, Mojo stops and prints an error with the line number. Read it, fix that line, and run once more.
Run Just Checks for main
mojo run expects your file to have a main function. Without one, it has nothing to start, and you get a clear error.
Building an Executable
For a faster standalone program, mojo build creates an executable file you can run directly, without compiling each time.
mojo build hello.mojorun vs build
Use run while learning and iterating, since it is one quick step. Reach for build when you want a finished binary to ship or reuse.
Your Workflow Is Set
Write code, save the file, type mojo run, read the output. That short cycle is the core habit of every Mojo session.
Quick Check
You want to compile and execute hello.mojo in a single terminal step. Which command does that?
Recap
Use mojo run from the file's folder to compile and execute in one step, read the output, then edit and run again. 🎯
常见问题解答
「使用 mojo run 运行」课时是免费的吗?
是的 — 「使用 mojo run 运行」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Mojo Academy 课程的其余内容,请升级到 CoddyKit PRO。 Mojo Academy 课程共包含 4 节课。
「使用 mojo run 运行」这节课中我会学到什么?
从命令行执行程序 你通过在浏览器中直接运行的动手代码来练习 Mojo Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Mojo Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Mojo Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「使用 mojo run 运行」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Mojo Academy 课中编写并运行代码吗?
能。每节 Mojo Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 您的第一个 .mojo 文件
- main 入口点
- 使用 mojo run 运行
- 在 Mojo REPL 中探索