登录
首页 >  Golang >  Go问答

无法点击“运行”按钮当访问 http://localhost:8080/wasm_exec.html

来源:stackoverflow

时间:2024-03-08 13:33:25 423浏览 收藏

哈喽!今天心血来潮给大家带来了《无法点击“运行”按钮当访问 http://localhost:8080/wasm_exec.html 》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

我是 stackoverflow 和 golang web assembly 的新手

我对这个问题感到困惑:我编写了一个测试代码

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 的逐步 hello world
web/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,然后选择 console 选项卡,现在是时候单击 run 按钮,然后您将在浏览器控制台中看到 hello world

今天关于《无法点击“运行”按钮当访问 http://localhost:8080/wasm_exec.html 》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>