【问题标题】:Syscall Constant syscall.ENONET Undefined in Go系统调用常量 syscall.ENONET 在 Go 中未定义
【发布时间】:2020-05-30 22:18:25
【问题描述】:

我尝试运行以下 bar.go 脚本

package main

import (
    "fmt"
    "syscall"
)

func main() {
    fmt.Printf("%d\n", uintptr(syscall.ENONET))
}

通过调用go run bar.go 得到这个错误:

# command-line-arguments
./bar.go:9:29: undefined: syscall.ENONET

我正在使用 Mac 并转到版本 1.14.3 darwin/amd64。我尝试在 Go 操场上运行这个确切的脚本,https://play.golang.org/p/ecMZPsGgGOa,它成功了。

我尝试使用CGO_ENABLED=1 GOOS=linux GOARCH=amd64 运行脚本,却得到了这个错误:

fork/exec /var/folders/2l/dj6ph5t92y17vhtv3n6xzr5r0000gn/T/go-build847134732/b001/exe/bar: exec format error

如何让syscall.ENONET 在 Mac 上工作?

谢谢

【问题讨论】:

  • 你的 Mac 不是 ARM。
  • 好的,尝试过amd64,但遇到了同样的问题

标签: macos go system-calls


【解决方案1】:

是的,它有一些奇怪的问题。虽然,syscall 现在已被锁定。

Deprecated: this package is locked down. 
Callers should use the corresponding package in the golang.org/x/sys repository instead.

我尝试使用go version go1.14.3 darwin/amd64,但遇到了同样的问题。

但在文档中,我们有:

ENONET          = Errno(0x40)

因为,Errno 也是导出类型,您可以显式模拟相同的行为:

package main

import (
    "fmt"
    "syscall"
)

func main() {
    fmt.Printf("%d\n", syscall.Errno(0x40)) // 64
    fmt.Printf("%v\n", syscall.Errno(0x40)) // host is down
}

【讨论】:

  • 并且无需启用CGO:CGO_ENABLED=1
猜你喜欢
  • 2015-02-06
  • 2019-06-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-26
  • 2010-09-09
  • 2014-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多