golang构建约束排除了所有Go文件。

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

golang build constraints exclude all Go files in

问题

我构建了一个简单的“Hello, World”wasm应用程序,如下所示:

GOARCH=wasm GOOS=js go build -o lib.wasm main.go

一切都很好。但是,我想在我的本地(Linux)环境中测试我的代码。所以我将它改为一个相当标准的形式:

package main

import "fmt"

func main() {
    fmt.Println("test")
}

然后运行简单的命令:

go run .

但是现在我得到了以下错误:

package xxx: build constraints exclude all Go files in xxx

我理解,如果我指定了一个wasm构建,就不能期望在本地运行它。但是我现在并不打算进行wasm构建。我的.go源文件中没有构建约束,并且我的go.mod文件只包含包名和go版本。

如果我将这些文件复制到一个新目录,并且只运行go run .而不进行wasm构建,一切都正常。

而且,如果我运行:

go build filename.go

它可以正常工作。

就好像有些东西“记住了”这个目录应该使用wasm构建,但是我找不到是什么/在哪里。

是否有一个缓存或其他地方记住了一些构建设置,我需要清除它们?我已经尝试过go cleango clean -cache,但没有成功!

英文:

I build a simple "Hello, World" wasm app like:

GOARCH=wasm GOOS=js go build -o lib.wasm main.go

All is well. But then I want to test my code in my local (Linux) environment. So I change it to a pretty standard:

package main

import "fmt"

func main () {
	fmt.Println("test")
}

And do a simple:

go run .

but now I get:

package xxx: build constraints exclude all Go files in xxx

I understand that if I specify a wasm build, I can't expect to run it locally. But I'm not trying to do a wasm build anymore. There are no build constraints in my .go source file, and my go.mod file just has the package and go version.

If I copy these file to a new directory and only do the go run . without the wasm build - all is good.

And if I do:

go build filename.go

it works properly.

It's like something "remembers" that this directory was supposed to be built with wasm - but I cant find what/where.

Is there a cache or something elsewhere that's remembering some build settings that I need to clear? I've tried go clean and go clean -cache - no luck!

答案1

得分: 3

答案似乎是,如果文件名以wasm.go结尾,Go语言会默认认为这是一个wasm文件。

这意味着如果你没有指定正确的操作系统和架构,普通的构建或运行将会失败,但是如果你使用一个明确的文件名(即使它以wasm.go结尾),运行将会正常工作。

???

英文:

The answer seems to be that if a filename ends in wasm.go - go will assume this is indeed a wasm file.

This means that a normal build or run will fail (if you don't specify proper OS and arch) - though a run with an explicit filename (even if it ends in wasm.go will work correctly.

???

huangapple
  • 本文由 发表于 2022年5月21日 22:50:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/72330627.html
匿名

发表评论

匿名网友

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

确定