# Example output

Output for `testdata/example` (`example.com/app/main` imports `example.com/app/mathx`).

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

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

This tree is goesm's primary output. `emit-ts` writes it to `goesm-ts/` (change with `-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 and the other runtime files
```

### 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 };
```

The entry package's `Result` is exported through a wrapper, `$Result$js1`, which converts arguments and results for JavaScript callers and turns a nil dereference into a Go panic ([js-exports.md](https://goesm.dev/reference/js-exports.md)); Go code calls `Result` itself. Each module ends with an inline TS→Go source map (`//# sourceMappingURL=data:...`), also written next to it as `<module>.ts.map`.

### Using it from a bundler or a TypeScript-aware runtime

```ts
// index.ts in a Vite project, or run directly: 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` bundles the same tree with esbuild. With `-split` it writes `dist/<import path>.js` per Go package plus `dist/@goesm/runtime/index.js` and `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
```

The `sources` of `dist/example.com/app/main.js.map` point at `main.go` (`mathx.js.map`: `mathx.go`), and `sourcesContent` holds the Go source.

## Using it from JavaScript

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

## Lowering examples (excerpts from 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: the result is fixed at return, then defers run
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 live in the runtime's 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: dispatch through the type descriptor's method table
function Format(v: $rt.Iface | null): string {
  return $rt.icall(v, "String");
}

// goroutine/channel: only blocking functions are 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];
}
```
