Skip to content

ES 模块 ​

脚本通过 import / export 组织,相对路径以当前文件为基准解析。lib.js:

js
export const PI = 3.14
export function double(x) { return x * 2 }

main.js:

js
import { PI, double } from "./lib.js"

setTimeout(() => console.log("tick"), 10)
console.log(double(PI))
text
$ gox main.js
6.28
tick

也支持默认导出、命名空间导入与动态 import():

js
import sq, { PI } from "./math_utils.js"     // 默认导出 + 命名导出
import * as math from "./math_utils.js"      // 命名空间

import("./math_utils.js").then(m => console.log(m.PI))   // 动态加载
限制说明
只有 ES 模块没有 CommonJS:require / module.exports / __dirname 都不存在
无 npm 解析没有 node_modules 查找、没有裸包名,只有相对路径与 gx/* / gox 内置模块
导入别名import { x as y } 支持,见语言边界

Apache License 2.0 · 用 ❤ 和 Go 编写