網站首頁 編程語言 正文
基本環境
有時需要做一些前端的數據處理,但是又不想把數據出來的方式就這么簡單的暴露在js里,然后就用了wasm來包裝這個處理函數,當然,這樣也能提高性能。
新建文件 index.js
const fastify = require('fastify')({ logger: true }) const path = require('path') // Serve the static assets fastify.register(require('fastify-static'), { root: path.join(__dirname, ''), prefix: '/' }) const start = async () => { try { await fastify.listen(8080, "0.0.0.0") fastify.log.info(`server listening on ${fastify.server.address().port}`) } catch (error) { fastify.log.error(error) } } start()
package.json
{ "scripts": { "dev": "node index.js" }, "dependencies": { "fastify": "^3.6.0", "fastify-static": "^3.2.1" } }
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>hello</title> </head> <body> hello </body> </html>
運行 npm run dev 打開http://127.0.0.1:8080
wasm部分
新建 go.mod
module hello-world go 1.18
main.go
package main import ( "syscall/js" ) func main() { message := "?? Hello World ??" document := js.Global().Get("document") h2 := document.Call("createElement", "h2") h2.Set("innerHTML", message) document.Get("body").Call("appendChild", h2) <-make(chan bool) }
運行 go env
win下
GOOS=windows
GOARCH=amd64
需要配置環境變量為 win 下設置 cmd運行 set GOOS=js
set GOARCH=wasm
生成必要文件(cmd會報錯 powershell可以執行) cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" .
會多出一個wasm_exec.js
的文件
go打包成wasm 運行go build -o main.wasm
運行結束后會生成一個名為main.wasm
的文件
然后修改之前的index.html
文件
<html> <head> <meta charset="utf-8"/> <script src="wasm_exec.js"></script> </head> <body> <h1>WASM</h1> <script> // 判斷是否支持instantiateStreaming加載 if (!WebAssembly.instantiateStreaming) { WebAssembly.instantiateStreaming = async (resp, importObject) => { const source = await (await resp).arrayBuffer() return await WebAssembly.instantiate(source, importObject) } } // 異步加載wasm文件 function loadWasm(path) { const go = new Go() return new Promise((resolve, reject) => { WebAssembly.instantiateStreaming(fetch(path), go.importObject) .then(result => { go.run(result.instance) resolve(result.instance) }) .catch(error => { reject(error) }) }) } //加載wasm文件 loadWasm("main.wasm").then(wasm => { console.log("wasm已加載 ??") }).catch(error => { console.log("加載出錯了", error) }) </script> </body> </html>
然后刷新瀏覽器就能看到這么一個界面
原文鏈接:https://juejin.cn/post/7133612627531399198
相關推薦
- 2022-06-12 Python?colorama?彩色打印實現代碼_python
- 2022-03-15 pycharm安裝opencv出現錯誤:Could not find a version that
- 2022-03-13 C語言打印各種圖案實例代碼_C 語言
- 2022-08-17 R語言學習VennDiagram包繪制韋恩圖示例_R語言
- 2022-12-29 Kotlin?Lambda表達式實踐使用介紹_Android
- 2023-07-26 TypeScript中的模塊與命名空間
- 2022-07-11 pandas實現按照多列排序-ascending_python
- 2023-02-09 使用adb?or?fastboot命令進入高通的9008(edl)模式的兩種方法_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支