Golang 1.18-特定平台的排除包

发布于 2025-02-13 04:33:59 字数 808 浏览 0 评论 0原文

我整天都在尝试在我的代码中应用构建约束: 我有一个旨在在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒几许 2025-02-20 04:33:59

我无法在评论中写下整个内容,但是让我知道这个小样本是否对您有帮助:

utils_darwin.go:

package utils

import "fmt"

func Test() {
    fmt.Println("Test from mac")
}

utils_linux.go

package utils

import "fmt"

func Test() {
    fmt.Println("Test from linux")
}

utils_windows.go main.go

package utils

import "fmt"

func Test() {
    fmt.Println("Test from windows")
}

main.go

package main

import "github.com/ninadingole/go-dev-stuff/platform/utils"

func main() {
    utils.Test()
}

在Mac上编译二进制文件时

GOOS=darwin go build -o prog ./platform

./prog

Test from mac

当我 在Linux的Docker中构建二进制文件,并获得以下输出,

Test from linux

让我知道这是否适合您,否则我将删除答案:D

I couldn't write the entire thing in the comments but let me know if this small sample helps you:

utils_darwin.go:

package utils

import "fmt"

func Test() {
    fmt.Println("Test from mac")
}

utils_linux.go

package utils

import "fmt"

func Test() {
    fmt.Println("Test from linux")
}

utils_windows.go

package utils

import "fmt"

func Test() {
    fmt.Println("Test from windows")
}

main.go

package main

import "github.com/ninadingole/go-dev-stuff/platform/utils"

func main() {
    utils.Test()
}

When I compile the binary on mac and run it like:

GOOS=darwin go build -o prog ./platform

./prog

Test from mac

I tried to build the binary in docker for linux and got the below output

Test from linux

Let me know if this works for you otherwise I will delete the answer :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文