duansha7453 2019-04-01 08:53
浏览 105
已采纳

如何进行:从多个包中生成字符串常量?

I have a struct like this:

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

In main.go I declare the type:

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

In each constants.go I have soimething like this:

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

How can I generate stringer with values from all packages?

  • 写回答

1条回答 默认 最新

  • doudou8893 2019-04-01 12:03
    关注

    golang.org/x/tools/cmd/stringer doesn't support this. Quoting from its doc:

    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.

    Easiest solution would be to put all enum values in the same package. You may use separate files, but they must be in the same package.

    If you want / must use multiple packages, you can only do this with stringer if you use different types, each defined in the package in which you list the constants.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题