golang

Go programmmig language

package main

import "fmt"

func main() {
        fmt.Println("Hello, 世界")
}

WebAssembly

build.sh

go build -o main main.go
GOOS=js GOARCH=wasm go build -o main.wasm main.go

index.html

<html>
        <head>
                <meta charset="utf-8"/>
                <script src="wasm_exec.js"></script>
                <script>
                        const go = new Go();
                        WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then((result) => {
                                go.run(result.instance);
                        });
                </script>
        </head>
        <body>
        <h1 id="datetimeHeading">5555</h1>
        <canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;">
        </canvas> 
        </body>
</html>

main.go

package main

//import "fmt"
import "time"
import "syscall/js"

func showTime(){
  for x := 0; ; x++ { // infinite loop
    currentTime := time.Now()
    // console.log in the browser
    //fmt.Printf("Hello, WebAssembly %s  ! \n" , currentTime.String() )
    document := js.Global().Get("document")
    document.Call("getElementById", "datetimeHeading").Set("innerHTML", currentTime.String())
    //document.getElementById("datetimeHeading").innerHTML="aaa"
    time.Sleep( 2 * time.Second  )
  }
}

func main() {
  go showTime()  // go routine executes the called function as a lightweight thread 

  document := js.Global().Get("document")
  c := document.Call("getElementById","myCanvas")
  ctx := c.Call("getContext","2d")
  ctx.Call("beginPath")
  ctx.Call("arc","95", "50", "40", "0", "6.283")
  ctx.Call("stroke");

  time.Sleep(24*time.Hour)
}

golang (last edited 2020-06-20 23:03:06 by localhost)