Go Web Assembly在输入http:// localhost:8080 / wasm_exec.html时无法单击Run

问题描述 投票:0回答:1

我对stackoverflow和golang Web程序集不熟悉

我为问题而苦恼:我编写了测试代码

main.go

package main

func main(){
    println("hello world")
}

并且我已经生成main.wasm并将wasm_exec.js和wasm_exec.html导入到工作目录中

我也写了一个网络服务器

package main

import (
    "flag"
    "log"
    "net/http"
)

var (
    listen = flag.String("listen", ":8080", "listen address")
    dir    = flag.String("dir", ".", "directory to serve")
)

func main() {
    flag.Parse()
    log.Printf("listening on %q...", *listen)
    err := http.ListenAndServe(*listen, http.FileServer(http.Dir(*dir)))
    log.Fatalln(err)
}

我使用Chrome并输入http://localhost:8080/wasm_exec.html

根据在线资料,有一个“运行”按钮可以单击。

但是,实际上我无法单击此按钮

我对此感到非常困惑

这是我关于stackoverflow的第一个问题。

抱歉英语不好

go webassembly
1个回答
0
投票

从Go Webassembly逐步Hello Worldweb/main.go文件:

package main

import (
    "log"
    "net/http"
)

func main() {
    http.Handle("/", http.FileServer(http.Dir(".")))
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

[main.go文件:

package main

import "fmt"

func main() {
    fmt.Println("Hello World")
}

在Linux终端中运行以下命令(对于Windows操作系统,将cp替换为copy,将$GOROOT替换为%GOROOT%):

GOOS=js GOARCH=wasm go build -o test.wasm
cp $GOROOT/misc/wasm/wasm_exec.html ./
cp $GOROOT/misc/wasm/wasm_exec.js ./

go run web/main.go

打开http://127.0.0.1:8080/wasm_exec.html

现在打开浏览器控制台(在浏览器内部右键单击,然后选择Inspect),现在是时候单击Run按钮,然后您会看到hello world

© www.soinside.com 2019 - 2024. All rights reserved.