从项目目录的根目录运行 gogenerate 时,我在使用 gogenerate 生成 grpc 服务器时遇到一些问题。
当我运行时go generate -v它只返回main.go. 但是,这些指令是在子包之一中定义的。如果我go generate在子包中运行,它会按预期工作。我希望导入能够确保go generate找到子包并运行指令。
该项目具有以下结构:
cmd/
root.go
run.go
pkg/
subpkg/
protobuf/
proto1.proto
subpkg.go
main.go
Run Code Online (Sandbox Code Playgroud)
subpkg.go 的内容
//go:generate protoc -I ./protobuf --go_out=plugins=grpc:./protobuf ./protobuf/proto1.proto
package subpkg
Run Code Online (Sandbox Code Playgroud)
main.go 的内容:
cmd/
root.go
run.go
pkg/
subpkg/
protobuf/
proto1.proto
subpkg.go
main.go
Run Code Online (Sandbox Code Playgroud)
在 run.go 包中,我导入了包 subpkg。
如何确保 gogenerate 可以从项目的根目录运行并执行所有子包中的所有指令。
您正在寻找go generate ./...。
每go help generate:
usage: go generate [-run regexp] [-n] [-v] [-x] [build flags] [file.go... | packages]
...
For more about specifying packages, see 'go help packages'.
Run Code Online (Sandbox Code Playgroud)
go help packages:
Many commands apply to a set of packages:
go action [packages]
Usually, [packages] is a list of import paths.
An import path that is a rooted path or that begins with
a . or .. element is interpreted as a file system path and
denotes the package in that directory.
Otherwise, the import path P denotes the package found in
the directory DIR/src/P for some DIR listed in the GOPATH
environment variable (For more details see: 'go help gopath').
If no import paths are given, the action applies to the
package in the current directory.
...
An import path is a pattern if it includes one or more "..." wildcards,
each of which can match any string, including the empty string and
strings containing slashes. Such a pattern expands to all package
directories found in the GOPATH trees with names matching the
patterns.
Run Code Online (Sandbox Code Playgroud)
因此,当您没有为需要包的 Go 命令指定包时,它会假定该包是当前目录。子目录是不同的包,因此不包括在内。“此包及其下子目录中的所有包递归地”的一个方便的简写是./...,如
go get ./...
go generate ./...
go test ./...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4099 次 |
| 最近记录: |