* string和sql.NullString之间的区别 [英] Difference between *string and sql.NullString

查看:59
本文介绍了* string和sql.NullString之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在Golang中努力处理SQL NULL值.尝试使用 Decode() sql.NullString 取消JSON对象的编组失败后,我在StackOverflow上得到了以下答案:

I've been strugguling with SQL NULL values in Golang recently. After trying to unsuccessfully unmarshal JSON objects using Decode() and sql.NullString, I came to this answer on StackOverflow :

将null分配给JSON字段而不是Golang中的空字符串

使用字符串指针的解决方案似乎可以与Decode()和nil值完美配合.那么 sql.NullString * string 有什么区别?只是零检查吗?

The solution using a string pointer seems to work perfectly with Decode() and nil values. So what is the difference between sql.NullString and *string ? Is it just about nil checking ?

推荐答案

SQL的空值与Golang不同.

SQL has different null values than Golang.

如果您查看 sql.NullString 的定义,那么您将获得以下信息:

If you look at the definition of sql.NullString then this is what you get:

type NullString struct {
    String string
    Valid  bool // Valid is true if String is not NULL
}

如您所见, sql.NullString 是一种表示来自SQL的空字符串(对应于"NULL")的方法.另一方面,nil * string 是指向nil的字符串的指针,因此两者是不同的.

As you can see, sql.NullString is a way to represent null string coming from SQL (which correspond to "NULL"). On the other hand, a nil *string is a pointer to a string which is nil, so the two are different.

这篇关于* string和sql.NullString之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆