go-wasm-driver
v0.1.2
Published
## usage
Downloads
4
Readme
go-wasm-driver
usage
go wasm
func main() {
wait := make(chan struct{}, 0)
// 统一挂载在同一个全局变量
js.Global().Get("__axe__").Set("stringMd5", js.FuncOf(StringMd5))
js.Global().Get("__axe__").Set("uint8ArrayMd5", js.FuncOf(Uint8ArrayMd5))
<-wait
}
func StringMd5(this js.Value, p []js.Value) interface{} {
txt := p[0].String()
sum := md5.Sum([]byte(txt))
return hex.EncodeToString(sum[:])
}
func Uint8ArrayMd5(this js.Value, p []js.Value) interface{} {
startTime := time.Now()
buffer := make([]byte, p[0].Length())
js.CopyBytesToGo(buffer, p[0])
sum := md5.Sum(buffer)
md5 := hex.EncodeToString(sum[:])
endTime := time.Now()
duration := endTime.Sub(startTime)
fmt.Printf("时间差:%v\n", duration)
return md5
}
import { getAbility } from 'go-wasm-driver';
// 将 go wasm 打包成图片
import wasm from './module.png';
import { useEffect } from 'react';
async function run() {
const { stringMd5 } = await getAbility('__axe__', wasm);
console.log(stringMd5('abc'));
}
api
type getAbility = (globalName: string, wasmPath: string): Promise<any>
- globalName: go 中设置挂载的全局变量
- wasmPath: 打包的 wasm 文件,建议打包成 png 图片