【问题标题】:imports syscall/js: build constraints exclude all Go files in /usr/local/go/src/syscall导入 syscall/js:构建约束排除 /usr/local/go/src/syscall 中的所有 Go 文件
【发布时间】: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


    【解决方案1】:

    syscall/js 包确实有构建约束:

    // +build js,wasm
    

    你需要用correct GOOS and GOARCH options构建程序:

    GOOS=js GOARCH=wasm go build -o main.wasm
    

    【讨论】:

      猜你喜欢
      • 2022-10-18
      • 1970-01-01
      • 2021-07-10
      • 2013-05-07
      • 1970-01-01
      • 2013-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多