我对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逐步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
),现在是时候单击Run
按钮,然后您会看到hello world
。