go - 导入 syscall/js:构建约束排除 /usr/local/go/src/syscall 中的所有 Go 文件
问题描述
我正在尝试通过将 Go 函数转换为 Web 程序集来在 javascript 中使用 Go api-call 函数。为此,我正在尝试导入syscall/js
,但会引发以下错误:
导入 syscall/js:构建约束排除 /usr/local/go/src/syscall/js 中的所有 Go 文件
package main
import (
"fmt"
"io/ioutil"
"net/http"
"syscall/js" // I can't use syscall/js
)
func main() {
fmt.Println("Go Web Assembly")
js.Global().Set("getData", getData)
}
func getData(string, error) {
resp, err := http.Get("https://jsonplaceholder.typicode.com/posts")
if err != nil {
return
}
// We Read the response body on the line below.
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
// Convert the body to type string
sb := string(body)
return sb
}
解决方案
该syscall/js
包确实有一个构建约束:
// +build js,wasm
您需要使用正确的GOOS
和GOARCH
选项构建程序:
GOOS=js GOARCH=wasm go build -o main.wasm
推荐阅读
- actions-on-google - Google Dialogflow 上的“完成您的请求时出错”
- java - 在数组中找到最大值;在 Java 中使用四个线程
- java - 接口中常见默认方法的菱形问题
- mysql - 具有多个可选参数的 MySQL 存储过程
- android - 在查看 [src/nullnull/debug, src/debug/nullnull, src/nullnull, src/debug, src/nullnullDebug] 时找不到 google-services.json
- c# - 递归文件搜索缺少子目录
- algorithm - 计算时间复杂度
- angular - http-server -p 8080 不服务于我的角度应用程序。而 ng serve 确实
- javascript - 如何在动画 amcharts 饼图中显示月份?
- java - 离线保存应用评分