duanqinqiao4844 2015-11-01 01:46
浏览 688
已采纳

在Golang中使用sql.NullFloat64与* float64?

I'm using gorm to define my db table schema. Noticed that gorm itself uses time.Time for created_at and *time.Time for deleted_at. I'm assuming this is because deleted_at has cases of being NULL.

I also noticed that for many Golang ORMs they use sql.NullFloat64 for storing db values that could be a float64 or NULL. Why not just use *float64 instead of sql.NullFloat64? What's the difference?

type Geo struct {
    latitude *float64
    longitude sql.NullFloat64
}
// What is the difference between latitude and longitude in this case?
  • 写回答

1条回答 默认 最新

  • dongrunying7537 2015-11-01 06:31
    关注

    From Russ Cox (biggest Go's contributor according to Github) : https://groups.google.com/forum/#!topic/golang-nuts/vOTFu2SMNeA

    There's no effective difference. We thought people might want to use NullString because it is so common and perhaps expresses the intent more clearly than *string. But either will work.

    I'm thinking though that using a pointer might give the GC one more thing to track. It probably depends on the usage.

    On some very simple code, using -gcflags=-m to build, the escape analysis does say that new(float64) escapes to heap (here is the dummy code I used: https://play.golang.org/p/K4sQaXyQKG).

    Also, when debugging, printing a struct containing a sql.NullSomething ({value:{Float64:1 Valid:true}}) looks better than a struct containing a pointer ({value:0xc82000a420}).

    So I'd recommend the usage of the sql.Null* structs.

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

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面