3

int类型会自动化为0,如何表示没有值(类似java中的null)呢?

 2 years ago
source link: https://studygolang.com/articles/35180
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

int类型会自动化为0,如何表示没有值(类似java中的null)呢?

xjie_jack · 3天之前 · 248 次点击 · 预计阅读时间不到 1 分钟 · 大约8小时之前 开始浏览    

方法一:用指针*int,可以判断是否为nil,如果不为nil,再取指针变量的值;
方法二:可以用sql.NullInt32,判断Valid为true,再取Int32的值。
方法三:用int表示状态时,可以在设计的时候规定不使用0值,即从1开始表示具体的状态值。避免0和nil的歧义。

来源:gostudy


有疑问加站长微信联系(非本文作者))

280

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK