go wasm windows 平台编译通过

原文链接: go wasm windows 平台编译通过

上一篇: goland cannot find module for path [已解决]

下一篇: go wasm fib 对比原生js

go nb!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

有点大啊,之前只有100k左右, 这咋直接1M了

https://my.oschina.net/ahaoboy/blog/3045690

up-017feab9680b13f7bdfff9d67d1b19ba694.pngup-1a06d660c7cc72832f5f65a5d828a62c732.png

hello world

fib.go

编译时一定要注意命令后面不能有空格, 否则报错

up-a9b9e7467c2117101d03291c35ba6c45e29.png

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>

up-c0a1e14ae8ef8f63decdf5e712943c773b6.png

简单加法

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>

up-5a1416d41d4281a877a58bab31d1dd89f1a.png

操作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>

up-d0bab3e5de622c3ef321a271ea8164cd254.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值