Golang 1.18 – 排除特定平台的包

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

Golang 1.18 - Exclude package for specific platform

问题

我整天都在尝试在我的代码中应用一个构建约束:
我有一个代理程序,设计用于在Windows和Linux上工作,但是我必须与Windows注册表进行交互(显然在Linux上没有等效物)。

我的包导入了:
> "golang.org/x/sys/windows/registry"

该项目在Windows上构建,但在Linux上不构建。
然后我了解到了构建约束://go:build windows(或!linux)
我最初使用了// +build windows,但我发现它是为旧版本的Go设计的。

这是我的包文件的头部:

//go:build windows

package utils

import (
	"fmt"
	"log"

	"golang.org/x/sys/windows/registry"
)
...

我在Windows和Linux上都使用VSCode,我还可以在我的go.mod文件中看到一个引用。
请问有什么帮助吗?

当我在Linux上运行上述代码时,仍然会得到以下错误:

> build golang.org/x/sys/windows/registry: cannot load
> golang.org/x/sys/windows/registry: no Go source files (exit status 1)

**编辑:**我使用默认的VSCode工具来编译和运行我的项目,但是我尝试了go build .,仍然得到相同的错误。
此外,_windows.go和_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.

答案1

得分: 2

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

utils_darwin.go:

package utils

import "fmt"

func Test() {
	fmt.Println("来自 Mac 的测试")
}

utils_linux.go:

package utils

import "fmt"

func Test() {
	fmt.Println("来自 Linux 的测试")
}

utils_windows.go:

package utils

import "fmt"

func Test() {
	fmt.Println("来自 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

输出为:

来自 Mac 的测试

我尝试在 Docker 中为 Linux 构建二进制文件,并得到以下输出:

来自 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 Golang 1.18 – 排除特定平台的包

huangapple
  • 本文由 发表于 2022年7月4日 03:07:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/72849222.html
匿名

发表评论

匿名网友

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

确定