A module in Mojo is analogous to a module in languages like Python; it's a single file containing code (functions, structs, etc.) that can be imported and reused in other Mojo files. Key points include:
MyPair, with associated methods.main() function, as their primary purpose is to be imported into other files that drive the application logic.mymodule.mojomojoCopy code
struct MyPair:
var first: Int
var second: Int
fn __init__(inout self, first: Int, second: Int):
self.first = first
self.second = second
fn dump(self):
print(self.first, self.second)
struct MyPair defines a simple structure with two integers.__init__ is a constructor that initializes a MyPair instance.dump is a method that prints the values of the instance.You can import modules using the import statement or from ... import ... syntax, similar to Python. The examples show three ways to import and use the MyPair struct from mymodule.mojo:
from mymodule import MyPairimport mymoduleimport mymodule as myA package in Mojo is a collection of modules in a directory, which must include an __init__.mojo file to be recognized as a package. This structure allows for more organized code and can be compiled into a package file for easier distribution.
__init__.mojo file is essential for Mojo to treat a directory as a package..mojopkg or .📦 files for portability.