使用Go編寫WebAssembly

(Windows10 WSL) html

 

 

I. Install go

 

    see 《install go on ubuntu

 

II. go wasm

 

1. compile

set GOARCH=wasm
set GOOS=js
go build -o helloworld.wasm helloworld.go

 

2. set mime type

/etc/mime.types
Ln132:python

application/wasm        wasm

 

 

3. run


(1) other filesgit


1) wasm_exec
from E:\progFiles\go\misc\wasm
to e:\go-projectgithub

 

2) helloworld.htmlgolang

<html>
    <head>
        <meta charset="utf-8">
        <script src="wasm_exec.js"></script>
        <script>
            const go = new Go();
            WebAssembly.instantiateStreaming(fetch("helloworld.wasm"), go.importObject).then((result) => {
                go.run(result.instance);
            });
        </script>
    </head>
    <body></body>
</html>

 

 

(2) execubuntu

cd /mnt/e/go-project

 

python -m SimpleHTTPServer 8080

 

http://localhost:8080/helloworld.htmlapp

 

 

src: helloworld.go工具

package main

import (  
    "fmt"  
)


func main() {
    var str string
    var i = 1
    str = "Hello, WebAssembly!"
    
    for i < 10 {
        fmt.Println(str, i)
        i++
    }
}

 開發者工具--》Consolepost

 

 

Reference:fetch

    https://github.com/golang/go/wiki/WebAssembly

相關文章
相關標籤/搜索