Importando arquivos com @import
Traga outro arquivo-fonte para o escopo.
Importando arquivos com @import é uma aula grátis de Zig Academy no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Zig Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Zig Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
Bring In Another File
Code grows, so you split it across files. The built-in @import pulls another Zig source file into the current one. 📦
const math = @import("math.zig");It Returns a Value
@import is an expression that gives back a value. You store it in a const, then reach into it with dot access just like a struct.
const math = @import("math.zig");
const r = math.add(2, 3);A File Is a Struct
Every Zig file is secretly a struct. Its top-level declarations become the fields and functions you access after importing it.
Only pub Crosses the Line
A declaration is reachable from another file only if it is marked pub. Without pub, it stays private to its own file.
pub fn add(a: i32, b: i32) i32 {
return a + b;
}Paths Are Relative
A file path inside @import is relative to the importing file. So "util/text.zig" looks in a util folder beside the current file.
const text = @import("util/text.zig");Import the Standard Library
The most common import is std. It is a special module name, not a file path, that gives you the whole standard library.
const std = @import("std");The builtin Module
Another special name is builtin. It exposes compile-time info like the target os and the current optimize mode.
const builtin = @import("builtin");Imports Run at Comptime
@import is resolved entirely at compile time. The path must be a string literal known then, never a value computed at run time.
Cached, Not Duplicated
Import the same file from ten places and Zig loads it once. Every import of one path shares the exact same struct value.
Rename for Clarity
Because the result is just a const, you can rename it freely. Pick a short, clear alias that reads well at every call site.
const fs = @import("std").fs;Reach In Without Storing
You can also chain straight off an import for a one-off use, skipping the intermediate const when you only need one item.
const out = @import("std").io.getStdOut();Quick Check
Recall what makes a top-level item usable from another file.
Recap
You learned that @import loads a file as a struct at compile time, std and builtin are special names, and only pub items cross over. 🎯
Perguntas Frequentes
A aula “Importando arquivos com @import” é grátis?
Sim — o texto completo de “Importando arquivos com @import” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Zig Academy, atualize para CoddyKit PRO. O curso de Zig Academy inclui 4 aulas no total.
O que vou aprender em “Importando arquivos com @import”?
Traga outro arquivo-fonte para o escopo. Você pratica Zig Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Zig Academy?
Nenhuma experiência prévia é necessária. Zig Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.
Quanto tempo leva a aula “Importando arquivos com @import”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Zig Academy?
Sim. Cada aula de Zig Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Importando arquivos com @import
- Estruturando um projeto com vários arquivos
- build.zig.zon e o gerenciador de pacotes
- Adicionando um módulo de terceiros