go語(yǔ)言打包的網(wǎng)頁(yè)wasm示例詳解
基本環(huán)境
有時(shí)需要做一些前端的數(shù)據(jù)處理,但是又不想把數(shù)據(jù)出來(lái)的方式就這么簡(jiǎn)單的暴露在js里,然后就用了wasm來(lái)包裝這個(gè)處理函數(shù),當(dāng)然,這樣也能提高性能。
新建文件 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>
運(yùn)行 npm run dev 打開(kāi)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)
}
運(yùn)行 go env win下
GOOS=windows
GOARCH=amd64
需要配置環(huán)境變量為 win 下設(shè)置 cmd運(yùn)行 set GOOS=js set GOARCH=wasm
生成必要文件(cmd會(huì)報(bào)錯(cuò) powershell可以執(zhí)行) cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" . 會(huì)多出一個(gè)wasm_exec.js的文件
go打包成wasm 運(yùn)行go build -o main.wasm 運(yùn)行結(jié)束后會(huì)生成一個(gè)名為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("加載出錯(cuò)了", error)
})
</script>
</body>
</html>
然后刷新瀏覽器就能看到這么一個(gè)界面

以上就是go打包網(wǎng)頁(yè)wasm示例詳解的詳細(xì)內(nèi)容,更多關(guān)于go打包網(wǎng)頁(yè)wasm的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Go語(yǔ)言輕量級(jí)線(xiàn)程Goroutine用法實(shí)例
這篇文章主要介紹了Go語(yǔ)言輕量級(jí)線(xiàn)程Goroutine用法,實(shí)例分析了goroutine使用技巧,需要的朋友可以參考下2015-02-02
Go語(yǔ)言中break label與goto label的區(qū)別
這篇文章主要介紹了Go語(yǔ)言中break label與goto label的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
golang 微服務(wù)之gRPC與Protobuf的使用
這篇文章主要介紹了golang 微服務(wù)之gRPC與Protobuf的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Go語(yǔ)言數(shù)據(jù)類(lèi)型詳細(xì)介紹
這篇文章主要介紹了Go語(yǔ)言數(shù)據(jù)類(lèi)型詳細(xì)介紹,Go語(yǔ)言數(shù)據(jù)類(lèi)型包含基礎(chǔ)類(lèi)型和復(fù)合類(lèi)型兩大類(lèi),下文關(guān)于這兩類(lèi)型的相關(guān)介紹,需要的小伙伴可以參考一下2022-03-03
Golang map實(shí)踐及實(shí)現(xiàn)原理解析
這篇文章主要介紹了Golang map實(shí)踐以及實(shí)現(xiàn)原理,Go 語(yǔ)言中,通過(guò)哈希查找表實(shí)現(xiàn) map,用鏈表法解決哈希沖突,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-06-06
Go語(yǔ)言轉(zhuǎn)化php數(shù)組的示例代碼
這篇文章主要為大家詳細(xì)介紹了Go語(yǔ)言如何實(shí)現(xiàn)轉(zhuǎn)化php數(shù)組的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),對(duì)我們深入學(xué)習(xí)GO語(yǔ)言有一定的幫助,需要的可以參考下2023-11-11

