2. Getting started

2.1. Getting Mojo

2.2. Hello World

It is a time honored tradition to start learning a programming language with a program printing "Hello, World!". Let’s start with that.

Please open a text editor of your choice and type the following and save the file as hello_world.mojo. The name of the file does not matter as long as you pass the same file to the compiler.

def main(): print("Hello, World!")

Now open a command line program and enter the following in its terminal.

mojo hello_world.mojo

You should see the following in the terminal.

Hello, World!

Congrats! You have executed your first Mojo program!

Alternate file extension

Mojo also supports file extension other than ".mojo". Instead of saving the file as hello_world.mojo, you can save it as hello_world.🔥 and run mojo hello_world.🔥.

2.3. Dissecting the program

Unlike many other system languages, Mojo has very minimal ceremony to define a simple program like Hello World. The following line (known as a statement) calls a function named print which is provided by Mojo. The text Hello, World! is placed inside double quotes. Any text placed inside double quotes (or single quotes for that matter) is considered by Mojo as a String Literal. Literals are constant values that do not change during the execution of the program and are provided directly to the program source code. In this case, we pass the value "Hello, World!" to the function print.

The print function knows how to take that passed value and to bring it into the screen.


In the file, there is def main():, which you can ignore for the moment. It will come up later on. In a future release of Mojo, the need for def main(): will be gone. *

2.4. Compiler

The command mojo hello_world.mojo you executed in the terminal took the Mojo source code and compiled it into a form that can be executed in the computer. The mojo command then directly executes the compiled form.

If you do not want to execute immediately the program, and just want to create an executable file, you can use the command mojo build hello_world.mojo. It then creates an executable file in the same directory as the source file.