Any reason to use sql.NullString rather than *string?

4,648 views
Skip to first unread message

Hǎiliàng

unread,
Feb 26, 2013, 9:26:48 AM2/26/13
to golan...@googlegroups.com
Doc says that Null* types in database/sql are introduced to support nullable value, but I have also tried to use *string, *int and *time.Time and it turns out that they can also be stored and retrieved from database and represents null.

Is there any reason to use these sql.Null* types rather than pointers to primitive types?

Hǎiliàng

Julien Schmidt

unread,
Feb 26, 2013, 11:14:21 AM2/26/13
to golan...@googlegroups.com
No this doesn't work. For example you can't assign nil to an int var http://play.golang.org/p/HmrSPO1mbR
How else should you return the nil value? Set the pointer address to nil?

Russ Cox

unread,
Feb 26, 2013, 12:19:51 PM2/26/13
to Hǎiliàng, golang-nuts
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.

Russ

Reply all
Reply to author
Forward
0 new messages