登录
首页 >  Golang >  Go问答

排除所有 Go 文件:在 macOS 上使用约束排除 Go 1.18.3

来源:stackoverflow

时间:2024-02-19 22:18:24 237浏览 收藏

最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《排除所有 Go 文件:在 macOS 上使用约束排除 Go 1.18.3》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~

问题内容

我是 go 和 terratest 的新手。我有以下地形测试

package main

import (
    "regexp"
    "testing"

    "github.com/gruntwork-io/terratest/modules/terraform"
    "github.com/stretchr/testify/assert"
)

func tests3creation(t *testing.t) {
    t.parallel()

    terraformoptions := terraform.withdefaultretryableerrors(t, &terraform.options{
        terraformdir: "./unit-test",
    })

    defer terraform.destroy(t, terraformoptions)

    terraform.initandapply(t, terraformoptions)

    bastionsecuritygroup := terraform.output(t, terraformoptions, "bastion_security_group")

    assert.regexp(t, regexp.mustcompile(`^sg-*`), bastionsecuritygroup)
}

我初始化如下:

go mod init github.com/myorg/terraform-bastion-linux

当尝试使用 go test -v 运行它时,出现错误:

package github.com/myorg/terraform-bastion-linux: build constraints exclude all go files in /users/george/terraform-bastion-linux/test

我的环境如下:

macos big sur 11.6.4
cpu: intel i9
terraform --version                                                                      
terraform v1.2.3
on darwin_amd64
go version
go version go1.18.3 darwin/amd64

我没有设置以 go 开头的环境变量,例如,env|grep go 不会返回任何结果。

正如以下建议:

  • 构建约束排除所有 go 文件
  • 构建约束排除所有 go 文件

我尝试在文件顶部添加以下内容

//+build darwin,cgo linux
//go:build (darwin && cgo) || linux

并且还导出 goos 和 goarch 环境变量

export GOOS=darwin
export GOARCH=amd64

但我仍然遇到同样的错误。

如何解决此问题? 我可以尝试什么才能成功运行测试?


正确答案


正如 JimB 所评论的,问题在于文件名包含单词 linux,Go 将其解释为构建约束,只能在 Linux 上运行,而不能在 Darwin 上运行。我是 Go 的新手,这些错误似乎具有极大的误导性。我什至没有在最初的问题中包含文件名,因为我认为这无关紧要。

以上就是《排除所有 Go 文件:在 macOS 上使用约束排除 Go 1.18.3》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>