原文链接: go wasm windows 平台编译通过
上一篇: goland cannot find module for path [已解决]
下一篇: go wasm fib 对比原生js
go nb!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
有点大啊,之前只有100k左右, 这咋直接1M了
https://my.oschina.net/ahaoboy/blog/3045690
hello world
fib.go
编译时一定要注意命令后面不能有空格, 否则报错
package main
import "fmt"
func main() {
fmt.Println("Hello World wasm")
}
// set GOARCH=wasm
// set GOOS=js
// go build -o lib.wasm main.go
拷贝wasm_exec.js
C:\Go\misc\wasm
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" .
index.html
<!DOCTYPE html>
<!--
Copyright 2018 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<html>
<head>
<meta charset="utf-8" />
<title>Go wasm</title>
</head>
<body>
<script src="./wasm_exec.js"></script>
<script>
if (!WebAssembly.instantiateStreaming) {
// polyfill
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}
const go = new Go();
let mod, inst;
WebAssembly.instantiateStreaming(fetch("./lib.wasm"), go.importObject).then(
result => {
mod = result.module;
inst = result.instance;
document.getElementById("runButton").disabled = false;
}
);
async function run() {
await go.run(inst);
inst = await WebAssembly.instantiate(mod, go.importObject); // reset instance
}
</script>
<button onClick="run();" id="runButton" disabled>Run</button>
</body>
</html>
简单加法
add.go
package main
import "syscall/js"
func add(this js.Value, i []js.Value) interface{} {
js.Global().Set("output", js.ValueOf(i[0].Int()+i[1].Int()))
var res = i[0].Int() + i[1].Int()
println("go add", i, res)
return js.ValueOf(res)
}
func subtract(this js.Value, i []js.Value) interface{} {
js.Global().Set("output", js.ValueOf(i[0].Int()-i[1].Int()))
var res = i[0].Int() - i[1].Int()
println("go sub", i, res)
return js.ValueOf(res)
}
func registerCallbacks() {
js.Global().Set("add", js.FuncOf(add))
js.Global().Set("subtract", js.FuncOf(subtract))
}
func main() {
c := make(chan struct{}, 0)
println("WASM Go Initialized")
// register functions
registerCallbacks()
<-c
}
add.html
<!DOCTYPE html>
<!--
Copyright 2018 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<html>
<head>
<meta charset="utf-8"/>
<title>Go wasm</title>
</head>
<body>
<script src="wasm_exec.js"></script>
<script>
if (!WebAssembly.instantiateStreaming) {
// polyfill
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}
const go = new Go();
let mod, inst;
WebAssembly.instantiateStreaming(fetch("./add.wasm"), go.importObject).then(
async result => {
mod = result.module;
inst = result.instance;
await go.run(inst);
}
);
function run() {
let addRes = add(1, 2)
console.log('js add', addRes, addRes + 1, typeof addRes)
console.log('js sub', subtract(1, 2))
}
</script>
<button onclick="run()">run</button>
</body>
</html>
操作dom
addDom.go
package main
import (
"strconv"
"syscall/js"
)
func add(this js.Value, i []js.Value) interface{} {
value1 := js.Global().Get("document").Call("getElementById", i[0].String()).Get("value").String()
value2 := js.Global().Get("document").Call("getElementById", i[1].String()).Get("value").String()
int1, _ := strconv.Atoi(value1)
int2, _ := strconv.Atoi(value2)
js.Global().Get("document").Call("getElementById", i[2].String()).Set("value", int1+int2)
return js.ValueOf(int1 + int2)
}
func registerCallbacks() {
js.Global().Set("add", js.FuncOf(add))
}
func main() {
c := make(chan struct{}, 0)
println("WASM Go Initialized")
// register functions
registerCallbacks()
<-c
}
addDom.html
<!DOCTYPE html>
<!--
Copyright 2018 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<html>
<head>
<meta charset="utf-8"/>
<title>Go wasm</title>
</head>
<body>
<script src="wasm_exec.js"></script>
<script>
if (!WebAssembly.instantiateStreaming) {
// polyfill
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}
const go = new Go();
let mod, inst;
WebAssembly.instantiateStreaming(fetch("./addDom.wasm"), go.importObject).then(
async result => {
mod = result.module;
inst = result.instance;
await go.run(inst);
}
);
</script>
<input type="text" id="value1"/>
<input type="text" id="value2"/>
<button onClick="add('value1', 'value2', 'result');" id="addButton">
Add
</button>
<button
onClick="subtract('value1', 'value2', 'result');"
id="subtractButton"
>
Subtract
</button>
<input type="text" id="result"/></body>
</html>