Golang 1.18-特定平台的排除包
我整天都在尝试在我的代码中应用构建约束: 我有一个旨在在Windows和Linux上使用的代理,但是,我必须与Windows注册表进行交互(显然在Linux上没有任何等效)。
我的软件包导入
“ golang.org/x/sys/windows/registry”
该项目在Windows上构建,而不是在Linux上。 然后,我了解了构建约束:// GO:构建Windows(或!linux) 我最初是从// +构建窗口开始的,但我看到它是针对较旧的GO版本的。
这是我的软件包文件的标题:
//go:build windows
package utils
import (
"fmt"
"log"
"golang.org/x/sys/windows/registry"
)
...
我在Windows和Linux上都使用VSCODE,我还可以在GO.Mod文件中看到一个引用。 请为此提供任何帮助吗?
当我运行上面的代码时,我仍然在Linux上获得以下内容:
构建golang.org/x/sys/windows/Registry:无法加载 golang.org/x/sys/windows/Registry:无GO源文件(退出状态1)
编辑:我使用默认的VSCODE工具来编译和运行我的项目,但是我尝试了:Go Build build 。而且我仍然遇到相同的错误。 另外,_windows.go and _linux.go后缀不合适,因为代理将来会成为Mac。
I spent the whole day trying to apply a build constraint in my code:
I have an agent that is designed to work on Windows and Linux, however, I have to interact with the Windows registry (which obviously doesn't have any equivalent on Linux).
My package imports
"golang.org/x/sys/windows/registry"
The project builds on Windows but not on Linux.
I then learnt about build constraints: //go:build windows (or !linux)
I initially started with // +build windows but I saw that it's for older Go versions.
Here is the header of my package file:
//go:build windows
package utils
import (
"fmt"
"log"
"golang.org/x/sys/windows/registry"
)
...
I use VSCode both on Windows and Linux, I can also see a reference to in my go.mod file.
Any help with this please?
When I run the code above, I still get the following on Linux:
build golang.org/x/sys/windows/registry: cannot load
golang.org/x/sys/windows/registry: no Go source files (exit status 1)
Edit: I use the default VSCode tools to compile and run my project, however I have tried: go build . and I still get the same error.
Also, the _windows.go and _linux.go suffixes are not appropriate as the agent would be fore Mac in the future.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法在评论中写下整个内容,但是让我知道这个小样本是否对您有帮助:
utils_darwin.go:
utils_linux.go
utils_windows.go main.go
main.go
在Mac上编译二进制文件时
当我 在Linux的Docker中构建二进制文件,并获得以下输出,
让我知道这是否适合您,否则我将删除答案:D
I couldn't write the entire thing in the comments but let me know if this small sample helps you:
utils_darwin.go:
utils_linux.go
utils_windows.go
main.go
When I compile the binary on mac and run it like:
I tried to build the binary in docker for linux and got the below output
Let me know if this works for you otherwise I will delete the answer :D