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

4 644 vaatamist
Liigu esimese lugemata sõnumi juurde

Hǎiliàng

lugemata,
26. veebr 2013, 09:26:4826.02.13
kuni 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

lugemata,
26. veebr 2013, 11:14:2126.02.13
kuni 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

lugemata,
26. veebr 2013, 12:19:5126.02.13
kuni 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

Vasta kõigile
Vasta autorile
Saada edasi
0 uut sõnumit