{"id":15085960,"url":"https://github.com/philippgille/go-wasm","last_synced_at":"2025-07-10T00:37:31.737Z","repository":{"id":189501643,"uuid":"680788144","full_name":"philippgille/go-wasm","owner":"philippgille","description":"Examples of how to work with WebAssembly and WASI in Go","archived":false,"fork":false,"pushed_at":"2023-10-03T14:36:34.000Z","size":29,"stargazers_count":8,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-17T00:41:32.769Z","etag":null,"topics":["browser","bun","deno","go","golang","node","nodejs","wasi","wasm","wasmer","wasmtime","wazero","web","webassembly"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/philippgille.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-20T12:07:49.000Z","updated_at":"2025-02-12T12:45:51.000Z","dependencies_parsed_at":"2024-06-21T19:10:29.956Z","dependency_job_id":"add9295b-c7e5-441b-b56f-09deea288e00","html_url":"https://github.com/philippgille/go-wasm","commit_stats":null,"previous_names":["philippgille/go-wasm"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/philippgille/go-wasm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philippgille%2Fgo-wasm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philippgille%2Fgo-wasm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philippgille%2Fgo-wasm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philippgille%2Fgo-wasm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/philippgille","download_url":"https://codeload.github.com/philippgille/go-wasm/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/philippgille%2Fgo-wasm/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264506265,"owners_count":23619005,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["browser","bun","deno","go","golang","node","nodejs","wasi","wasm","wasmer","wasmtime","wazero","web","webassembly"],"created_at":"2024-09-25T07:02:18.006Z","updated_at":"2025-07-10T00:37:31.714Z","avatar_url":"https://github.com/philippgille.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-wasm\n\nThis repo contains examples of how to work with WebAssembly and WASI in the Go ecosystem.\n\nRegular WASM is meant to be executed inside a browser (like Chrome or Firefox) or JavaScript runtime (like [Node](https://nodejs.org), [Deno](https://deno.land/) or [Bun](https://bun.sh/)). As such it only has access to [web APIs](https://developer.mozilla.org/en-US/docs/Web/API) or runtime-specific APIs.\n\nWASI on the other hand, as the name \"**W**eb**A**ssembly **S**ystem **I**nterface\" suggests, provides more direct access to the host system, like to the filesystem or network sockets. Similar to how the browser executes JavaScript and WebAssembly in a sandbox, WASI runtimes also execute WASI programs in a sandbox, requiring explicit permissions for things like file access.\n\n## Table of Contents\n\n- [WASM](#wasm)\n  - [Execute WASM directly](#execute-wasm-directly)\n    - [Browser](#browser)\n    - [Node](#node)\n    - [Deno](#deno)\n    - [Bun](#bun)\n  - [Embed WASM in Go](#embed-wasm-in-go)\n- [WASI](#wasi)\n  - [Execute WASI directly](#execute-wasi-directly)\n  - [Embed WASI in Go](#embed-wasi-in-go)\n    - [wazero](#wazero)\n    - [wasmtime](#wasmtime)\n    - [Wasmer](#wasmer)\n\n## WASM\n\nThis works with Go versions *prior* to 1.21, as it's \"regular\" WASM, not WASI.\n\nCompile the Go program to WebAssembly: `GOOS=js GOARCH=wasm go build -o go-wasm.wasm`\n\n### Execute WASM directly\n\nGo WASM execution requires a Go-specific wrapper, `wasm_exec.js`. This can then be executed by any JavaScript runtime, in or outside the browser. The Go project provides this in `$(go env GOROOT)/misc/wasm/wasm_exec.js`.\n\nThe non-browser runtimes differ in how files are read from the host's filesystem, so for reading the `wasm_exec.js` file, in addition to this file itself, we need to have another runtime-specific wrapper.\n\n#### Browser\n\nFor running the WASM program in the browser, you can serve the files with any web server that supports the `application/wasm` MIME type. You can use a regular Go server (`http.ListenAndServe(...)`), [Caddy](https://caddyserver.com/), or others.\n\n```bash\ncp go-wasm.wasm browser\ncp $(go env GOROOT)/misc/wasm/wasm_exec.js browser/wasm_exec.js\ncd browser\ncaddy file-server --listen :2015\n```\n\nThen in your browser visit \u003chttp://localhost:2015\u003e.\n\n\u003e Tested with Firefox 116.0.3\n\n#### Node\n\n```bash\ncp go-wasm.wasm node\ncp $(go env GOROOT)/misc/wasm/wasm_exec.js deno/wasm_exec.js\ncd node\nnode node.js\n```\n\nThe Go authors also provide a convenience script for this, so instead of copying the `wasm_exec.js` and writing our own `node.js` wrapper script, we can simply run:\n\n```bash\n$(go env GOROOT)/misc/wasm/go_js_wasm_exec go-wasm.wasm\n```\n\n\u003e Tested with [Node v18.17.1](https://github.com/nodejs/node/releases/tag/v18.17.1)\n\n#### Deno\n\n```bash\ncp go-wasm.wasm deno\ncp $(go env GOROOT)/misc/wasm/wasm_exec.js deno/wasm_exec.js\ncd deno\ndeno run --allow-read=./go-wasm.wasm deno.js\n```\n\nℹ️: Deno runs programs in a sandbox by default, so for reading the WASM file we need to grant the permission accordingly.\n\n\u003e Tested with [Deno 1.36.1](https://github.com/denoland/deno/releases/tag/v1.36.1)\n\n#### Bun\n\n```bash\ncp go-wasm.wasm bun\ncp $(go env GOROOT)/misc/wasm/wasm_exec.js deno/wasm_exec.js\ncd bun\nbun bun.js\n```\n\n\u003e Tested with [Bun 0.7.3](https://github.com/oven-sh/bun/releases/tag/bun-v0.7.3)\n\n### Embed WASM in Go\n\n🚧 TODO\n\n## WASI\n\nGo 1.21 has official WASI *preview1* support, so we can compile Go programs to WASM that execute outside of a browser / JavaScript runtime.\n\nCompile Go program to WASI: `GOOS=wasip1 GOARCH=wasm go build -o go-wasi.wasm`\n\n### Execute WASI directly\n\nAny WASM runtime with WASI support, on any OS, can then execute this file:\n\n- [wasmtime](https://wasmtime.dev/): `wasmtime go-wasi.wasm` (tested with [wasmtime CLI 11.0.0](https://github.com/bytecodealliance/wasmtime/releases/tag/v11.0.0))\n- [Wasmer](https://wasmer.io/): `wasmer run go-wasi.wasm` (tested with [Wasmer 4.1.1](https://github.com/wasmerio/wasmer/releases/tag/v4.1.1))\n- [wazero](https://wazero.io/): `wazero run go-wasi.wasm` (tested with [wazero 1.4.0](https://github.com/tetratelabs/wazero/releases/tag/v1.4.0))\n\n### Embed WASI in Go\n\nOr we run the WASI program with an embeddable WASM runtime from within another Go program. Most of the ⬆️ listed runtimes also have Go libraries for embedding.\n\n#### wazero\n\n```bash\ncp go-wasi.wasm embed-wasi/wazero\ncd embed-wasi/wazero\ngo run .\n```\n\n#### wasmtime\n\n🚧 TODO: \u003chttps://github.com/bytecodealliance/wasmtime-go\u003e\n\n#### Wasmer\n\n🚧 TODO: \u003chttps://github.com/wasmerio/wasmer-go\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilippgille%2Fgo-wasm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphilippgille%2Fgo-wasm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphilippgille%2Fgo-wasm/lists"}