Module imports
Modules can be imported using the import
keyword:
This program can use any public definitions from the os
module, such
as the input
function.
See the
standard library
documentation for a list of common modules and their public symbols.
By default, you have to specify the module prefix every time you call an external function. This may seem verbose at first, but it makes code much more readable and easier to understand – it is always clear which function from which module is being called. This is especially useful in large code bases.
Cyclic module imports are not allowed, like in Go.
Selective imports
You can also import specific functions and types from modules directly:
This will import the module as well. Also, this is not allowed for constants – they must always be prefixed.
You can import several specific symbols at once:
Module import aliasing
Any imported module name can be aliased using the as
keyword:
You cannot define methods for imported types. However, you can redeclare a type using type aliases and add methods for it: