Module
Module is the base unit of code organization in Move. Modules are used to group and isolate code, and all of the members of the module are private to the module by default. In this section you will learn how to define a module, how to declare its members and how to access them from other modules.
Module declaration
Modules are declared using the module
keyword followed by the package address, module name and the
module body inside the curly braces {}
. The module name should be in snake_case
- all lowercase
letters with underscores between words. Modules names must be unique in the package.
Usually, a single file in the sources/
folder contains a single module. The file name should match
the module name - for example, a donut_shop
module should be stored in the donut_shop.move
file.
You can read more about coding conventions in the
Coding Conventions section.
module book::my_module {
// module body
}
Structs, functions, constants and imports all part of the module:
Address / Named address
Module address can be specified as both: an address literal (does not require the @
prefix) or a
named address specified in the Package Manifest. In the example below,
both are identical because there's a book = "0x0"
record in the [addresses]
section of the
Move.toml
.
module 0x0::address_literal { /* ... */ }
module book::named_address { /* ... */ }
Addresses section in the Move.toml:
# Move.toml
[addresses]
book = "0x0"
Module members
Module members are declared inside the module body. To illustrate that, let's define a simple module with a struct, a function and a constant:
module book::my_module_with_members {
// import
use book::my_module;
// a constant
const CONST: u8 = 0;
// a struct
public struct Struct {}
// method alias
public use fun function as Struct.struct_fun;
// function
fun function(_: &Struct) { /* function body */ }
}
Further reading
- Modules in the Move Reference.