# 生成物の実例

`testdata/example` (`example.com/app/main` が `example.com/app/mathx` を import) の出力です。

再現: `cd testdata/example && go run ../../cmd/goesm emit-ts ./main` / `go run ../../cmd/goesm build -split ./main`

## 生成 TypeScript (`goesm emit-ts ./main`)

この tree が goesm の主な出力です。`emit-ts` は `goesm-ts/` に書き出します (`-o` で変更可能)。

```text
goesm-ts/
├── example.com/app/main.ts   (+ main.ts.map)
├── example.com/app/mathx.ts  (+ mathx.ts.map)
└── @goesm/runtime/           index.ts、natives.ts とその他の runtime file
```

### goesm-ts/example.com/app/mathx.ts

```ts
// Code generated by goesm from Go package example.com/app/mathx. DO NOT EDIT.
// Go semantics were checked by go/types and lowered by goesm; any ESM
// bundler (or TypeScript-aware runtime) can consume this module.
// @ts-nocheck
/* eslint-disable */
import * as $rt from "../../@goesm/runtime/index.ts";
const $t1 = /* @__PURE__ */ (() => $rt.funcOf([$rt.types.int, $rt.types.int], [$rt.types.int], false))();
function Add(a: number, b: number): number {
  return (a + b);
}
const $goesm = { path: "example.com/app/mathx", funcs: { Add: { fn: Add, type: $t1, async: false } } };
export { Add, $goesm };
```

### goesm-ts/example.com/app/main.ts

```ts
// Code generated by goesm from Go package example.com/app/main. DO NOT EDIT.
// Go semantics were checked by go/types and lowered by goesm; any ESM
// bundler (or TypeScript-aware runtime) can consume this module.
// @ts-nocheck
/* eslint-disable */
import * as $rt from "../../@goesm/runtime/index.ts";
import "./mathx.ts";
import * as mathx from "./mathx.ts";
const $t2 = /* @__PURE__ */ (() => $rt.funcOf([], [$rt.types.int], false))();
function Result(): number {
  return mathx.Add(1, 2);
}
function $Result$js1(): number {
  try {
    return Result();
  } catch (e) {
    throw $rt.toPanic(e);
  }
}
const $goesm = { path: "example.com/app/main", funcs: { Result: { fn: Result, type: $t2, async: false } }, toJS: $rt.toJS };
export { $Result$js1 as "Result", $goesm };
```

entry package の `Result` は、wrapper の `$Result$js1` を通して export されます。wrapper は JavaScript から呼ぶための引数と戻り値の変換を行い、nil の参照外しを Go の panic にします ([js-exports.ja.md](https://goesm.dev/ja/reference/js-exports.md))。Go のコードは `Result` そのものを呼びます。末尾には TS→Go の source map が `//# sourceMappingURL=data:...` として inline で付きます。同じ map は module の隣に `<module>.ts.map` としても書き出されます。

### bundler や TypeScript を扱える runtime から使う

```ts
// Vite project の index.ts、または直接実行: bun index.ts / node index.ts (Node.js 22.18 以上)
import { Result } from "./goesm-ts/example.com/app/main.ts";
console.log(Result()); // 3
```

## ESM (`goesm build -split ./main`)

`goesm build` は同じ tree を esbuild で bundle します。`-split` では Go package ごとの `dist/<import path>.js` と、`dist/@goesm/runtime/index.js`、`dist/@goesm/runtime/natives.js` を書き出します。

### dist/example.com/app/mathx.js

```js
// example.com/app/mathx.ts
import * as $rt from "../../@goesm/runtime/index.js";
var $t1 = /* @__PURE__ */ (() => $rt.funcOf([$rt.types.int, $rt.types.int], [$rt.types.int], false))();
function Add(a, b) {
  return a + b;
}
var $goesm = { path: "example.com/app/mathx", funcs: { Add: { fn: Add, type: $t1, async: false } } };
export {
  $goesm,
  Add
};
//# sourceMappingURL=mathx.js.map
```

### dist/example.com/app/main.js

```js
// example.com/app/main.ts
import * as $rt from "../../@goesm/runtime/index.js";
import "./mathx.js";
import * as mathx from "./mathx.js";
var $t2 = /* @__PURE__ */ (() => $rt.funcOf([], [$rt.types.int], false))();
function Result() {
  return mathx.Add(1, 2);
}
function $Result$js1() {
  try {
    return Result();
  } catch (e) {
    throw $rt.toPanic(e);
  }
}
var $goesm = { path: "example.com/app/main", funcs: { Result: { fn: Result, type: $t2, async: false } }, toJS: $rt.toJS };
export {
  $goesm,
  $Result$js1 as Result
};
//# sourceMappingURL=main.js.map
```

`dist/example.com/app/main.js.map` の `sources` は `main.go` を指し (`mathx.js.map` は `mathx.go`)、`sourcesContent` に Go source を含みます。

## JavaScript から使う

```js
import { Result } from "./dist/example.com/app/main.js";
Result(); // 3
```

## 意味論の lowering 例 (testdata/semantics より抜粋)

```ts
// pointer: value := 1; Set(&value)
function Set(v: any): void {
  $rt.deref(v).v = 10;
}
function PointerExample(): number {
  let value = $rt.cell(1);
  Set(value);
  return value.v;
}

// defer: return 時の値を確定してから defer を実行
function DeferExample(): $rt.S<number> {
  let $r0: $rt.S<number> = null;
  const $d = new $rt.Defers();
  $body: try {
    let result: $rt.S<number> = $rt.sliceLit([]);
    $d.defer((): void => {
      result = $rt.append(result, [3], () => 0);
    });
    result = $rt.append(result, [1], () => 0);
    result = $rt.append(result, [2], () => 0);
    $r0 = result;
    break $body;
  } catch ($e) { $d.fail($e); } finally { $d.run(); }
  return $r0;
}

// map: Go map semantics は runtime の GoMap
function Map(): $rt.S<$rt.Iface | null> {
  let m: $rt.M<string, number> = $rt.mapLit($rt.types.string, ["a", 1]);
  $rt.mapSet(m, "b", 2);
  const $1 = $rt.mapLookup(m, "a", () => 0);
  let value: number = $1;
  let ok: boolean = $rt.$R.r1;
  const $2 = $rt.mapLookup(m, "zzz", () => 0);
  let missing: number = $2;
  let ok2: boolean = $rt.$R.r1;
  $rt.mapDelete(m, "a");
  const $3 = $rt.mapLookup(m, "a", () => 0);
  let ok3: boolean = $rt.$R.r1;
  return $rt.sliceLit([$rt.box($rt.types.int, value), $rt.box($rt.types.bool, ok), $rt.box($rt.types.int, missing), $rt.box($rt.types.bool, ok2), $rt.box($rt.types.bool, ok3), $rt.box($rt.types.int, $rt.mapLen(m)), $rt.box($rt.types.int, $rt.mapGet(m, "b", () => 0))]);
}

// interface: 型 descriptor の method table で dispatch
function Format(v: $rt.Iface | null): string {
  return $rt.icall(v, "String");
}

// goroutine/channel: blocking 関数だけが async
async function Worker(ch: $rt.Chan<number> | null): Promise<void> {
  await $rt.send(ch, 42);
}
async function Example(): Promise<number> {
  let ch: $rt.Chan<number> | null = $rt.makeChan(0, () => 0);
  const $1 = ch;
  $rt.go(async () => { (await Worker($1)); });
  return (await $rt.recv(ch))[0];
}
```
