Null strings in Go

632 views
Skip to first unread message

Michael Hoisie

unread,
Nov 25, 2009, 4:36:49 PM11/25/09
to golang-nuts
Most languages support the concept of null string values. Java, C/C++/
C#, javascript, etc.

For interoperability reasons it would be useful if Go supported a null
string value.

For instance, in the json package, Marshal and Unmarshal can't really
work with null strings. If you run the following code, the result is
not really correct:

var a struct{ A string} { };
json.Unmarshal(`{"a": null}`, &a);
fmt.Printf("%q", a.A); // prints "", which is is not technically
correct. null != ""

There's also question recently on stack overflow about this:
http://stackoverflow.com/questions/1799374/how-to-have-a-function-with-a-nullable-string-parameter-in-go

Is there a plan to support null strings in Go?

- Mike

roger peppe

unread,
Nov 26, 2009, 4:59:15 AM11/26/09
to Michael Hoisie, golang-nuts
2009/11/25 Michael Hoisie <hoi...@gmail.com>:
> For interoperability reasons it would be useful if Go supported a null
> string value.

if you want a nullable string value, you can just use *string.

Ian Lance Taylor

unread,
Nov 26, 2009, 2:16:21 PM11/26/09
to roger peppe, Michael Hoisie, golang-nuts
Or []byte.

Ian
Reply all
Reply to author
Forward
0 new messages