Zig's import system is straightforward and relies on the @import
function and
pub
keyword (to make code accessible outside the current file).
Instead of a package concept, each file is its own distinct namespace. Declarations must be exported to be used elseware.
main.zig:
const std = @import("std");
const User = @import("user.zig").User;
pub fn main() void {
const user = User{
.power = 9001,
.name = "Goku",
};
std.debug.print("{s}'s power is {d}\n", .{ user.name, user.power });
}
user.zig
pub const User = struct {
power: u64,
name: []const u8,
};
If user.zig had instead exported multiple things we could use it like this:
const user = @import("models/user.zig");
const User = user.User;
const MAX_POWER = user.MAX_POWER