https://github.com/vicradon/golang-wasm-demo
A demo application for adding numbers using a wasm binary
https://github.com/vicradon/golang-wasm-demo
Last synced: 4 months ago
JSON representation
A demo application for adding numbers using a wasm binary
- Host: GitHub
- URL: https://github.com/vicradon/golang-wasm-demo
- Owner: vicradon
- Created: 2024-09-10T03:15:25.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-12-17T23:35:13.000Z (7 months ago)
- Last Synced: 2025-02-04T13:44:30.656Z (5 months ago)
- Language: JavaScript
- Homepage: https://vicradon.github.io/golang-wasm-demo/
- Size: 1.11 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# WASM: Go + JS Demo
This is a very simple demo of generating webassembly bytecode using the Go programming language and running the said bytecode in a browser and in a shell via wasmtime. The Go source code constitutes a simple sum-function that takes two numbers as arguments and returns their sum.

## Building the target
You can build the browser and WASI targets by running `make`. The build commands are already specified in the Makefile. If you don't have `make` installed, you can build the targets by running the two commands below:
```sh
# for wasmtime
GOOS=wasip1 GOARCH=wasm go build -o wasi/app.wasm wasi/main.go
# for browser
GOOS=js GOARCH=wasm go build -o web/browser.wasm main.go
```## Running the wasi target
You can run the wasi target directly on your machine using wasmtime. You must have wasmtime already installed and available on your shell's path. If you don't have wasmtime already, you can install it from the [official website](https://wasmtime.dev/) and then run it like so:
```sh
wasmtime wasi/app.wasm
```Expected output:
```
╰─$ wasmtime wasi/app.wasm
Hello Golang!
Pass two numbers to sum
```## Running the browser demo
The web directory of this repo contains HTML, JavaScript, and the wasm binary necessary for the browser demo. The main JavaScript file (index.js) loads wasm binary (browser.wasm) with the help of the the `wasm_exec.js` library provided by the Go team. You can run the browser demo by serving the content of the `web` directory. Here's a sample command to achieve this using Python's http.server:
```sh
python3 -m http.server -d web 4500
```This runs the app (shown on the initial screenshot) on port 4500 on all interfaces (0.0.0.0).