dreamer1231 2018-03-26 11:48
浏览 20
已采纳

何时对基本类型使用类型定义

My question is about the type definition in Go and when to use it for basic types.
Consider the following example.
I have this struct which represents a row from my Database:

type DBEntityAttribute struct {
    Id              uint64
    EntityId        uint64
    EndPointNumber  uint8
    AttributeNumber uint8
    ParentId        uint64
    Value           string
    Tag             int
    ContentType     sql.NullString
    Maturity        int
    Author          int
    AttributeType   string
    IsNegated       bool
}

The EntityId and AttributeNumber are properties I use in A LOT of other structs everywhere in the code.
Now I wanted to refactor it to something like this:

type EntityId uint64

type AttributeNumber uint8

type DBEntityAttribute struct {
        Id              uint64
        EntityId        EntityId
        EndPointNumber  uint8
        AttributeNumber AttributeNumber
        ParentId        uint64
        Value           string
        Tag             int
        ContentType     sql.NullString
        Maturity        int
        Author          int
        AttributeType   string
        IsNegated       bool
    }

This would allow me to change the type of the EntityId and AttributeNumber at one single place. Also, when I pass for example an entityId as a function parameter, I can now give it the specific EntityId type, instead of the anonymous uint64.

My question is now:

  • is this considered good Go code?
  • where should I stop, meaning should I declare a distinct type for every property that is used elsewhere in the code? Or only for the important ones?

I know this is probably subjective, but often the Go community agrees on certain patterns and I want to comply with their guidelines.

  • 写回答

1条回答 默认 最新

  • douce1368 2018-03-26 11:54
    关注

    There's no hard and fast rule about when to use a custom type. My personal rule would be:

    Use a custom type when there's a reason.

    Your reason, of wanting to change the underlying type in a single location seems like a good reason.

    Other good reasons would be:

    • To assign a custom JSON (un)marshaler, or other interface method
    • As documentation
    • In conjunction with constants, to act as an enum
    • Improved type safety (avoid accidentally assigning a FooID to a BarID)
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序