英文:
import "golang.org/x/tools/cmd/stringer" is a program, not an importable package
问题
我在编译导入了github.com/Microsoft/go-winio
包的应用程序时遇到了以下错误。这个错误来自于该包。可能出了什么问题?
..\..\GoLang\GOPATH\pkg\mod\github.com\!microsoft\go-winio@v0.6.0\tools.go:5:8: import "golang.org/x/tools/cmd/stringer" is a program, not an importable package
英文:
I encountered the following error when compiling my app, which imports the package github.com/Microsoft/go-winio
. The error is from that package. What could be wrong?
..\..\GoLang\GOPATH\pkg\mod\github.com\!microsoft\go-winio@v0.6.0\tools.go:5:8: import "golang.org/x/tools/cmd/stringer" is a program, not an importable package
答案1
得分: 1
tools.go @v0.6.0 的内容如下:
//go:build tools
package winio
import _ "golang.org/x/tools/cmd/stringer"
这个文件的目的是在 go.mod 中对 stringer
进行版本控制,并且允许运行 go generate ./...
而不需要调用 go get
。详细信息请参阅 How can I track tool dependencies for a module?。
在正常构建中,由于 //go:build tools
,这个文件将被排除在外。
导致这个错误的可能原因有两个。
1. 你的应用程序使用了 tools
标签
很可能是你的应用程序使用了 tools
标签进行构建。如果是这种情况,请选择另一个标签来构建你的应用程序。
我在下面的示例中看到了相同的错误信息:
$ go run --tags tools main.go
../../../go/pkg/mod/github.com/!microsoft/go-winio@v0.6.0/tools.go:5:8: import "golang.org/x/tools/cmd/stringer" is a program, not an importable package
package main
import (
"fmt"
winio "github.com/Microsoft/go-winio"
)
func main() {
p, _ := winio.DecodeExtendedAttributes([]byte{})
fmt.Println(p)
}
2. 你的 Go 版本太旧
另一个可能的原因是你的 Go 版本太旧(1.15 或更早),不支持识别 //go:build
。请升级到 Go 1.17 或更新的版本。
$ go1.16 run main.go
main.go:6:2: //go:build comment without // +build comment
$ go1.15 run main.go
../../../go/pkg/mod/github.com/!microsoft/go-winio@v0.6.0/tools.go:5:8: import "golang.org/x/tools/cmd/stringer" is a program, not an importable package
英文:
The content of tools.go @v0.6.0 is:
//go:build tools
package winio
import _ "golang.org/x/tools/cmd/stringer"
The purpose of this file is to version stringer
in go.mod and allow
go generate ./...
to be run without needing a go get
call. See How can I track tool dependencies for a module? for more details.
In a normal build, this file will be excluded thanks to //go:build tools
.
There are two possible reasons that result in this error.
1. Your app is built with the tools
tag
It's most likely that you happen to build your app with the tools
tag. If this is your case, please choose another tag for your app.
I see the same error message with the following demo:
$ go run --tags tools main.go
../../../go/pkg/mod/github.com/!microsoft/go-winio@v0.6.0/tools.go:5:8: import "golang.org/x/tools/cmd/stringer" is a program, not an importable package
package main
import (
"fmt"
winio "github.com/Microsoft/go-winio"
)
func main() {
p, _ := winio.DecodeExtendedAttributes([]byte{})
fmt.Println(p)
}
2. Your Go is too old
Another possible reason is that your Go is too old (1.15 or older) and it does not recognize //go:build
. Please upgrade Go to 1.17 or newer.
$ go1.16 run main.go
main.go:6:2: //go:build comment without // +build comment
$ go1.15 run main.go
../../../go/pkg/mod/github.com/!microsoft/go-winio@v0.6.0/tools.go:5:8: import "golang.org/x/tools/cmd/stringer" is a program, not an importable package
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论