未定义的错误导致 go test 失败。

huangapple go评论123阅读模式
英文:

Undefined errors causing go test to fail

问题

我在运行go test时遇到了问题,我设置了一个测试。当我运行go test时,另一个文件中的方法(不在测试文件中)会抛出以下错误:

# GoPackage [GoPackage.test]
./io.go:100:17: undefined: unix.EpollEvent
./io.go:101:22: undefined: unix.EpollEvent
./io.go:121:20: undefined: unix.EpollCreate1
./io.go:135:22: undefined: unix.EPOLLPRI
./io.go:140:16: undefined: unix.EpollCtl
./io.go:141:36: undefined: unix.EPOLL_CTL_ADD
./io.go:145:19: undefined: unix.EpollWait
FAIL    GoPackage [build failed]

注意:io.go 导入了golang.org/x/sys/unix,这是这些 unix 系统调用的来源。io.go 中的一个示例是:

var event unix.EpollEvent

如果我注释掉原始方法并创建一个虚拟版本来运行测试,测试将成功运行。

我在 MacOS 上构建/运行测试,我已经阅读到这些系统调用在 Mac 上不可用(https://github.com/golang/go/issues/30539)。

有没有办法解决在 Mac 上测试 go 构建的问题?有没有办法阻止这些消息阻止测试运行?

谢谢!

英文:

I'm running into an issue when running go test with a test I've set up. There is method in another file (not in the test file) that throws the following errors when I run go test:

# GoPackage [GoPackage.test]
./io.go:100:17: undefined: unix.EpollEvent
./io.go:101:22: undefined: unix.EpollEvent
./io.go:121:20: undefined: unix.EpollCreate1
./io.go:135:22: undefined: unix.EPOLLPRI
./io.go:140:16: undefined: unix.EpollCtl
./io.go:141:36: undefined: unix.EPOLL_CTL_ADD
./io.go:145:19: undefined: unix.EpollWait
FAIL    GoPackage [build failed]

Note: io.go has an import for golang.org/x/sys/unix which is where these unix syscalls come from. One example in io.go is:

var event unix.EpollEvent

If I comment out the original method and create a dummy version and run the test, the test runs successfully.

I'm building/running the test on a MacOS and I've read the syscalls aren't available on Mac (https://github.com/golang/go/issues/30539).

Is there a way to get around this issue of testing a go build on a Mac? Is there a way to suppress these messages from preventing the test from running?

Thanks!

答案1

得分: 2

你会根据平台的不同切换包含的文件版本,例如:

io_linux.go

//go:build linux
// +build linux


import (
    "golang.org/x/sys/unix"
)

func DoSomething() {
    unix.EpollCreate(10)
}

io_other.go

//go:build !linux
// +build !linux

func DoSomething() {
    // 什么都不做,不支持 EpollCreate
}

现在当你构建时,如果是 Linux,它将包含 io_linux.go,否则将包含 io_other.go

英文:

You'd want to switch out which version of that file is included depending on the platform, so e.g.

> io_linux.go

//go:build linux
// +build linux


import (
    "golang.org/x/sys/unix"
)

func DoSomething() {
    unix.EpollCreate(10)
}

> io_other.go

//go:build !linux
// +build !linux

func DoSomething() {
    // do nothing, EpollCreate not supported
}

and now when you build, if it's linux, it will include io_linux.go, otherwise it will include io_other.go

huangapple
  • 本文由 发表于 2023年8月9日 00:22:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定