in packages/jsii-pacmak/lib/targets/go/package.ts [485:544]
private emitJsiiPackage({ code }: EmitContext) {
const dependencies = this.packageDependencies.sort((l, r) =>
l.moduleName.localeCompare(r.moduleName),
);
const file = join(JSII_INIT_PACKAGE, `${JSII_INIT_PACKAGE}.go`);
code.openFile(file);
code.line(
`// Package ${JSII_INIT_PACKAGE} contains the functionaility needed for jsii packages to`,
);
code.line(
'// initialize their dependencies and themselves. Users should never need to use this package',
);
code.line('// directly. If you find you need to - please report a bug at');
code.line('// https://github.com/aws/jsii/issues/new/choose');
code.line(`package ${JSII_INIT_PACKAGE}`);
code.line();
const toImport: ImportedModule[] = [
JSII_RT_MODULE,
{ module: 'embed', alias: '_' },
];
if (dependencies.length > 0) {
for (const pkg of dependencies) {
toImport.push({
alias: pkg.packageName,
module: `${pkg.root.goModuleName}/${JSII_INIT_PACKAGE}`,
});
}
}
importGoModules(code, toImport);
code.line();
code.line(`//go:embed ${tarballName(this.assembly)}`);
code.line('var tarball []byte');
code.line();
code.line(
`// ${JSII_INIT_FUNC} loads the necessary packages in the @jsii/kernel to support the enclosing module.`,
);
code.line(
'// The implementation is idempotent (and hence safe to be called over and over).',
);
code.open(`func ${JSII_INIT_FUNC}() {`);
if (dependencies.length > 0) {
code.line('// Ensure all dependencies are initialized');
for (const pkg of this.packageDependencies) {
code.line(`${pkg.packageName}.${JSII_INIT_FUNC}()`);
}
code.line();
}
code.line('// Load this library into the kernel');
code.line(
`${JSII_RT_ALIAS}.Load("${this.assembly.name}", "${this.assembly.version}", tarball)`,
);
code.close('}');
code.closeFile(file);
}
}