Null time value in SQL results

blov · · 1608 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Is there any type similar to sql.NullString for time.Time? I&#39;m scanning SQLite query results that contain null values in a timestamp column.</p> <p>Thank you!</p> <hr/>**评论:**<br/><br/>kocekoga: <pre><p>There is implementation of this in lib/pg but you can copy it to your project if you want:</p> <pre><code>type NullTime struct { Time time.Time Valid bool // Valid is true if Time is not NULL } // Scan implements the Scanner interface. func (nt *NullTime) Scan(value interface{}) error { nt.Time, nt.Valid = value.(time.Time) return nil } // Value implements the driver Valuer interface. func (nt NullTime) Value() (driver.Value, error) { if !nt.Valid { return nil, nil } return nt.Time, nil } </code></pre></pre>iltempo: <pre><p>Great. This is exactly what I was looking for. I&#39;m wondering why it did not make it into the standard lib.</p></pre>carbocation: <pre><p>It&#39;s a good pattern to learn because you can use this pattern for any type, including your own custom types.</p></pre>HectorJ: <pre><p>The MySQL driver has a NullTime type : <a href="https://godoc.org/github.com/go-sql-driver/mysql#NullTime" rel="nofollow">https://godoc.org/github.com/go-sql-driver/mysql#NullTime</a></p> <p>I&#39;m not sure if that is compatible with SQLite date-times, but worst case you can see the implementation <a href="https://github.com/go-sql-driver/mysql/blob/master/utils.go#L447" rel="nofollow">here</a> and make your own for SQLite</p></pre>iltempo: <pre><p>Thanks. This is exactly the same implementation as on lib/pg. Took it over.</p></pre>bo-banane: <pre><p>No idea whether that&#39;d work, but can&#39;t you use a pointer?</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1608 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传