如何在Go中上传非空的0值?
来源:stackoverflow
时间:2024-03-16 21:09:31 310浏览 收藏
在 Go 中将非空 0 值上传到 psql 数据库时,可能会遇到“null value in column \"alarm\" violates not-null constraint”错误。这是因为 Go 无法将 0 识别为非空值。解决方法有两种: 1. 使用指针:将 `alarm` 声明为指针类型,并在初始化时将其设置为零值,如 `alarm: new(int)`。 2. 使用 `sql.nullint32`:使用 `sql.nullint32` 类型,并在扫描数据库值时将其转换为 `int`。
问题内容
我正在尝试将数据上传到 psql 数据库并收到以下消息:
error: null value in column \"alarm\" violates not-null constraint
我发现警报值是 0,不是 null,而是 0...但是 golang 无法识别这一点...有没有办法解决这个问题?
type hsdatum struct { id int64 uuid uuid.uuid hsassociationid int64 timestamp time.time `sql:",notnull"` value int `sql:",notnull"` alarm int `sql:",notnull"` createdat time.time updatedat time.time } hsdatum := hsdata.hsdatum{ hsassociationid: systemassoc.id, timestamp: starttime, value: 1, alarm: int(alarm), // 0 in this example }
添加了有关数据库逻辑的更多信息:
func (h *HsDatum) BeforeInsert(db orm.DB) error { timeNow := timeutil.TimeNow() h.CreatedAt = timeNow h.UpdatedAt = timeNow // Manage distinct alarm table by checking for new value // Note: HS data needs to arrive in chronological order by timestamp needsUpdate := false lastAlarm, err := hsdistinctalarms.GetHsDistinctAlarmByMostRecent(h.HsAssociationID) var timeGap time.Duration if database.IsEmptyTable(err) { // No values so mark it needsUpdate = true } else if err == nil { if lastAlarm.Alarm != h.Alarm { // New value so mark it needsUpdate = true } timeGap = h.Timestamp.Sub(lastAlarm.LastUpdated) } else { logrus.Errorf("Failed to get most recent distinct alarm: %s", err) } if needsUpdate { // Create new alarm newAlarm := hsdistinctalarms.HsDistinctAlarm{ HsAssociationID: h.HsAssociationID, Timestamp: h.Timestamp, Alarm: h.Alarm, LastUpdated: h.Timestamp, } err = hsdistinctalarms.CreateHsDistinctAlarm(&newAlarm) if err != nil { logrus.Errorf("Failed to create hs distinct alarm: %s", err) } } else { // Update timestamp lastAlarm.LastUpdated = h.Timestamp err = hsdistinctalarms.UpdateHsDistinctAlarm(&lastAlarm) if err != nil { logrus.Errorf("Failed to update hs distinct alarm: %s", err) } } return nil } // CreateHsDatum creates the resource. // It modifies the parameter in place and returns error information. func CreateHsDatum(h *HsDatum) error { return database.GetDB().Insert(h) }
解决方案
选项 1:
使用指针
例如
alarm *int `sql:",notnull"` ... alarm: new(int) // init with zero value or alarm: &alarm // set a pointer to the alarm variable (should be int)
丑吗?是的。
选项 2:
使用 sql.nullint32
例如
Alarm sql.NullInt32 `sql:",notnull"` ... var a sql.NullInt32 if err := a.Scan(alarm); err != nil { // handle error } ... Alarm: a ...
好了,本文到此结束,带大家了解了《如何在Go中上传非空的0值?》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习