【发布时间】:2021-08-16 06:13:53
【问题描述】:
我正在尝试通过将 Go 函数转换为 Web 程序集来在 javascript 中使用 Go api-call 函数。为此,我尝试导入 syscall/js,但它会引发以下错误:
imports 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
}
【问题讨论】:
标签: go webassembly