douchaqi3369 2014-05-28 17:12
浏览 70
已采纳

Go中的可打印枚举

In some cases, it's convenient for user interaction and debugging purposes to have a human-readable string representation of enums. So far, the best I have come up with is:

type ElementType int

const (
    Fire = ElementType(iota)
    Air
    Water
    Earth
)

var elementTypeMap = map[ElementType]string{
    Fire: "The Fiery Fire",
    Air: "The Airy Air",
    Water: "The Watery Water",
    Earth: "The Earthy Earth",
}

func (el ElementType) String() string {
    return elementTypeMap[el]
}

The above, allows me to use and pass the enum as an int, keeping its standard performance, and to easily print its string representation anywhere. The only drawback is that there is an amount of boilerplate code that adds up if you have many enum types: I would be rather happy to avoid it.

Is there a way, preferably an idiomatic one, to reduce the boilerplate code above?

  • 写回答

3条回答 默认 最新

  • duanqin9507 2014-05-28 17:19
    关注

    This looks dryer (and faster) :

    type ElementType int
    
    const (
        Fire = ElementType(iota)
        Air
        Water
        Earth
    )
    
    var elementnames = [...]string {
        "The Fiery Fire",
        "The Airy Air",
        "The Watery Water",
        "The Earthy Earth"
    }
    
    func (el ElementType) String() string {
        return elementnames[el]
    }
    

    Note that there was a discussion on golang-nuts on whether giving a generic solution to assign names to enum constants and as far as I know it wasn't seen as necessary (see https://groups.google.com/forum/#!topic/golang-nuts/fCdBSRNNUY8).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dqfkd82886 2014-05-28 17:47
    关注

    There are alternative ways. For example, using a type elementType struct { name string } and var Fire = &elementType{"The Fiery Fire"} comes to my mind. You will still just pass and compare pointers, but you can attach arbitrary data to it.

    I do not exactly know your use-case, but I would probably do it in a similar way:

    type ElementType int
    
    const (
        Fire = ElementType(iota)
        Air
        Water
        Earth
    )
    
    func (e ElementType) String() string {
        var names = [...]string{
            Fire:  "The Fiery Fire",
            Air:   "The Airy Air",
            Water: "The Watery Water",
            Earth: "The Earthy Earth",
        }
        return names[e]
    }
    

    A map is computationally quite expensive. You can use an array and still list the elements in an arbitrary way. Also, making the array a local variable might make later changes easier. And you should probably also add an if e < 0 || e >= len(names) there, especially if your ElementType is exported.

    评论
  • dongzhan5286 2014-05-29 23:08
    关注

    I don't think arrays and maps are the way to go here. You have to manually define the elements anyway, so I think switch is the clearest. Like a map, and unlike an array, it lets you use non-dense constants (meaning constants other than 0,1,2,3...). It also allows you to define a case in case something goes terribly wrong and you use a non-constant; at the very least you can define a custom panic message.

    func (et ElementType) String() string {
        switch et {
        case Water:
            return "The Watery Water"
        case Air:
            return "The Airy Air"
        case Fire:
            return "The Fiery Fire"
        case Earth:
             return "The Earthy Earth"
        default:
             return "Heart has been banned from the elements"
        }
    }
    

    I'd also wager that the compiler can do some fancy optimizations with this that it can't do with array/map access since those are declared using var rather than const.

    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 在工控机(Ubuntu系统)上外接USB蓝牙硬件进行蓝牙通信
  • ¥15 关于PROCEDURE和FUNCTION的问题
  • ¥100 webapi的部署(标签-服务器)
  • ¥20 怎么加快手机软件内部计时的时间(关键词-日期时间)
  • ¥15 C语言除0问题的检测方法
  • ¥15 为什么四分管的内径有的是16mm有的15mm,四分不应该是12.7mm吗
  • ¥15 macos13下 ios交叉编译的问题
  • ¥15 bgz压缩文件怎么打开
  • ¥15 封装dll(引入了pcl的点云设计库)
  • ¥30 关于#开发语言#的问题:我需要在抄板的基础上再抄板抄程序,根据RDA8851CM基础上开发