gpt4 book ai didi

go - 如何去 :generate stringer constants from multiple packages?

转载 作者:IT王子 更新时间:2023-10-29 02:07:06 26 4
gpt4 key购买 nike

我有一个这样的结构:

.foo/bar/constants.go
.foo/constants.go
.main.go

main.go 中声明类型:

package agepack
type EventType uint
//go:generate stringer -type EventType
const (
FirstType EventType iota
SecondType
....
)

在每个 constants.go 中我都有这样的东西:

package foo
const (
OneMoreType agepack.EventType = 100 + iota
)

如何使用所有包的值生成 stringer?

最佳答案

golang.org/x/tools/cmd/stringer不支持这个。引用其文档:

With no arguments, it processes the package in the current directory. Otherwise, the arguments must name a single directory holding a Go package or a set of Go source files that represent a single Go package.

最简单的解决方案是将所有枚举值放在同一个包中。您可以使用单独的文件,但它们必须在同一个包中。

如果你想/必须使用多个包,你只能用 stringer 如果你使用不同的类型,每个都在你列出常量的包中定义。

关于go - 如何去 :generate stringer constants from multiple packages?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55451280/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com