Importar archivos con @import
Incluya otro archivo fuente en el ámbito.
Importar archivos con @import es una lección gratuita de Zig Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Zig Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Zig Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en 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. 🎯
Preguntas frecuentes
¿La lección «Importar archivos con @import» es gratis?
Sí — el texto completo de «Importar archivos con @import» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Zig Academy, actualiza a CoddyKit PRO. El curso de Zig Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Importar archivos con @import»?
Incluya otro archivo fuente en el ámbito. Practicas Zig Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Zig Academy?
No se requiere experiencia previa. Zig Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.
¿Cuánto tiempo toma la lección «Importar archivos con @import»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Zig Academy?
Sí. Cada lección de Zig Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Importar archivos con @import
- Estructuración de un proyecto con varios archivos
- build.zig.zon y el gestor de paquetes
- Añadir un módulo de terceros