0% found this document useful (0 votes)
57 views2 pages

Getting Started with Mojo Programming

The document discusses how to create and run a basic 'Hello World' program in the Mojo programming language. It explains how to set environment variables, run code in the Mojo REPL, build and run source files from the command line, and build executable binaries.

Uploaded by

Temo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views2 pages

Getting Started with Mojo Programming

The document discusses how to create and run a basic 'Hello World' program in the Mojo programming language. It explains how to set environment variables, run code in the Mojo REPL, build and run source files from the command line, and build executable binaries.

Uploaded by

Temo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
  • Running Code in Mojo
  • Setting Environment Variables
  • Conclusion

Welcome back to the Mojo Programming language course

After installing Mojo, you can use the Mojo CLI to build and compile Mojo programs.
Here’s how you can create the classic “Hello, world!” program:

Setting Environment Variables:

You need to set the MODULAR_HOME and PATH environment variables as described in the
output when you ran modular install mojo. If you’re using bash or zsh, add the
following lines to your configuration file (.bash_profile, .bashrc, or .zshrc):

export MODULAR_HOME="$HOME/.modular"

export PATH="$MODULAR_HOME/pkg/[Link].com_mojo/bin:$PATH"

Then source the file you just updated, for example:

source ~/.bash_profile

Running Code in the REPL:

You can run some code in the Mojo REPL, which allows you to write and run Mojo code
directly in a command prompt. To start a REPL session, type mojo in your terminal
and press Enter. Then type print("Hello, world!") and press Enter twice (a blank
line is required to indicate the end of an expression).

Building and Running Mojo Source Files:

You can quickly execute a Mojo file by passing it to the mojo command, or you can
build a compiled executable with the mojo build command.

Running a Mojo File:

Create a file named [Link] and add the following code:

Python

AI-generated code. Review and use carefully. More info on FAQ.

fn main():

print("Hello, world!")

Save the file and return to your terminal. Now run it with the mojo command:

mojo [Link]

Building an Executable Binary:

Create a stand-alone executable with the build command:

mojo build [Link]

Then run the executable:

./hello

Next Steps:
If you’re developing in VS Code, install the Mojo extension for features like code
completion, quick fixes, and hover help for Mojo APIs. If you’re new to Mojo, read
the Mojo language basics. If you want to package your code as a library, read about
Mojo modules and packages. If you want to explore some Mojo code, clone the Mojo
repo to see some examples. For a deep dive into the language, check out the Mojo
programming manual. To see all the available Mojo APIs, check out the Mojo standard
library reference.

Thanks for your attention, see you in the next lecture.

Common questions

Powered by AI

To build a Mojo executable binary, first create a source file like 'hello.mojo' with your code. Execute 'mojo build hello.mojo' to compile the file into a binary. After the build process, run the executable using './hello' command from the terminal .

The REPL environment benefits a programmer as it allows for immediate execution and testing of Mojo code snippets. This instant feedback helps in quickly verifying logic, exploring language features, and debugging without the need for setting up extensive project files or running cumbersome compile commands .

You can execute a simple 'Hello, world!' program in Mojo through several methods. First, using the Mojo REPL, start a session by typing 'mojo' in the terminal and then enter 'print("Hello, world!")' followed by an extra Enter to signify the end of the expression. Alternatively, create a source file named 'hello.mojo' with a 'fn main(): print("Hello, world!")' code and run it using the command 'mojo hello.mojo'. Lastly, you can also compile it into an executable binary using 'mojo build hello.mojo' and execute './hello' .

To set up the environment for running Mojo programs, you need to set the MODULAR_HOME and PATH environment variables. Add these lines to your configuration file (.bash_profile, .bashrc, or .zshrc): 'export MODULAR_HOME="$HOME/.modular"' and 'export PATH="$MODULAR_HOME/pkg/packages.modular.com_mojo/bin:$PATH"'. Then, you need to source the updated file by running the command 'source ~/.bash_profile' .

Setting environment variables like MODULAR_HOME and PATH influences the running of Mojo commands by specifying the directories where the Mojo binaries and libraries reside. This configuration ensures that the command-line interface can locate the necessary executables and libraries needed to compile and run Mojo code, allowing for seamless operations when invoking Mojo-specific commands .

Exploring the Mojo repo is beneficial as it provides developers with practical examples and code patterns applied in real-world scenarios. It offers insight into best practices, common coding structures, and innovative solutions to complex problems, serving as inspiration and a learning tool to broaden understanding and application of Mojo in varied programming situations .

The Mojo programming manual serves as an essential resource for new developers, offering an in-depth look into the language's features, syntax, and best practices. It guides developers through more complex aspects beyond basic tutorials, fostering a deeper understanding and enabling proficiency and confidence in using Mojo for more advanced projects .

Utilizing the Mojo standard library reference enhances a developer's efficiency by providing comprehensive API documentation. This allows developers to quickly find the necessary functions and their usages, streamlining the coding process and reducing the need for trial-and-error, thus improving coding speed and accuracy .

The Mojo extension for VS Code offers features such as code completion, quick fixes, and hover help for Mojo APIs. These functionalities are beneficial as they enhance productivity, reduce errors, and assist developers in writing efficient Mojo code by providing immediate feedback and contextual assistance .

Sourcing the shell configuration file after setting environment variables for Mojo is crucial because it reloads the shell's configuration and applies any changes to PATH or other environment settings. Skipping this step can lead to the system not recognizing the newly set environment variables immediately, which can cause command failures when trying to run Mojo commands until the terminal session is restarted or sourced manually .

 Welcome back to the Mojo Programming language course 
After installing Mojo, you can use the Mojo CLI to build and compile M
If you’re developing in VS Code, install the Mojo extension for features like code 
completion, quick fixes, and hover help f

You might also like