利用Go编写WebAssembly计算器(数量取胜的go语言学习法 )

利用Go编写WebAssembly计算器(数量取胜的go语言学习法 )_第1张图片

A simple calculator written in TinyGo and compiled to WebAssembly

Live Demo

简体中文

源代码

A simple calculator written in TinyGo and compiled to WebAssembly

bilibili 在线吹水

⚽️⚽️利用Go编写WebAssembly计算器、⚽️⚽️

Features

  • ⚡️ Vue 3+Vite+pnpm, 怎么快怎么来
  • TinyGo 追求最小打包体积
  • wasm 玩的就是wasm
  • Golang, of course
  • ☁️ Deploy on Netlify

snapshot

  • 挂载全局变量

const go = new Go(); // Defined in wasm_exec.js. Don't forget to add this in your index.html.

const importObject = go.importObject;
let wasmModule;

export const InitWasm = async () => {
    wasmModule = await wasmBrowserInstantiate(wasmUrl, importObject);
    go.run(wasmModule.instance);
}

export const ExpressionIt = (s) => {
    let result
    try {
        result = window.expression(s)
    } catch (e) {
        console.log(e)
        result = 'ERROR'
    }
    return result
};

  • vue端导入

(async function () {
    await InitWasm().then(() => {
        createApp(App).mount('#app');
    });
})();
  • ts支持

export function InitWasm(): Promise;
export function ExpressionIt(s: string): string;

你可能感兴趣的:(gowasm)