Meaning of Zero Value and IsValid()

748 views
Skip to first unread message

gofake

unread,
Feb 2, 2015, 6:09:38 AM2/2/15
to golan...@googlegroups.com
Hi there,

I'm very new at go, so I hope my question will be very simple for you. 

I don't understand the meaning of the Zero Value in connection with IsValid.

Excerpt from the Spec:
When storage is allocated for a variable, either through a declaration or a call of new, or when a new value is created, either through a composite literal or a call of make, and no explicit initialization is provided, the variable or value is given a default value. Each element of such a variable or value is set to the zero value for its type

Excerpt from reflect:
IsValid returns true if v represents a value. It returns false if v is the zero Value. 

If I get it right, the zero Value is set by default when I allocate a new variable and IsValid checks if the value is the zero Value. 


But why is only "myInterface" from the example false and all others true? If only an empty interface returns false, why do I need a special function to check this (I mean IsValid). Isn't it the same to check if myInterface == nil?

I hope you can help me with my issue.

Thanks!!!





Péter Szilágyi

unread,
Feb 2, 2015, 2:13:14 PM2/2/15
to gofake, golang-nuts
There's a slight semantic you skipped over:

IsValid returns true if v represents a value. It returns false if v is the zero Value. 

Capital V. It checks whether v is or is not the zero value of reflect.Value, not some random value in general.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Kastorff

unread,
Feb 2, 2015, 2:19:45 PM2/2/15
to golan...@googlegroups.com
Additionally, "ValueOf(nil) returns the zero Value." (docs for
reflect.ValueOf).

All the other variables you pass into ValueOf have a concrete type, so
the interface{}'s (type, concrete) pair contains a non-nil dynamic type,
making the interface{} value that ValueOf recieves non-nil.

Try putting each of those into an interface{} variable and test them ==
nil; you'll see that only the one that has nil dynamic type (the
interface{} variable) is actually equal to nil.

On 02/02/2015 11:13 AM, Péter Szilágyi wrote:
> There's a slight semantic you skipped over:
>
> IsValid returns true if v represents a value. It returns false if v
> is the zero *V*alue.
>
>
> Capital V. It checks whether v is or is not the zero value of
> reflect.Value, not some random value in general.
>
> On Mon, Feb 2, 2015 at 1:09 PM, gofake <fab...@gmx.de
> <mailto:fab...@gmx.de>> wrote:
>
> Hi there,
>
> I'm very new at go, so I hope my question will be very simple for you.
>
> I don't understand the meaning of the Zero Value in connection with
> IsValid.
>
> Excerpt from the Spec:
>
> When storage is allocated for a variable
> <https://golang.org/ref/spec#Variables>, either through a
> declaration or a call of |new|, or when a new value is created,
> either through a composite literal or a call of |make|, and no
> explicit initialization is provided, the variable or value is
> given a default value. Each element of such a variable or value
> is set to the /zero value/ for its type
>
>
> Excerpt from reflect:
>
> IsValid returns true if v represents a value. It returns false
> if v is the zero Value.
>
>
> If I get it right, the zero Value is set by default when I allocate
> a new variable and IsValid checks if the value is the zero Value.
>
> https://play.golang.org/p/pF09NBNXeR
>
> But why is only "myInterface" from the example false and all others
> true? If only an empty interface returns false, why do I need a
> special function to check this (I mean IsValid). Isn't it the same
> to check if myInterface == nil?
>
> I hope you can help me with my issue.
>
> Thanks!!!
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to golang-nuts...@googlegroups.com
> <mailto:golang-nuts...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to golang-nuts...@googlegroups.com
> <mailto:golang-nuts...@googlegroups.com>.

fab...@gmx.de

unread,
Feb 2, 2015, 2:56:21 PM2/2/15
to golan...@googlegroups.com
Thanks for your answers!!! But I still do not understand properly.

When do I have to check if isValid? Is there a common use case, or why isn't it enough to compare with nil? 

Sorry for my stupid questions.
>     For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to golang-nuts...@googlegroups.com

Ian Lance Taylor

unread,
Feb 2, 2015, 4:26:44 PM2/2/15
to fab...@gmx.de, golang-nuts
On Mon, Feb 2, 2015 at 11:56 AM, <fab...@gmx.de> wrote:
>
> When do I have to check if isValid? Is there a common use case, or why isn't
> it enough to compare with nil?

The most common case where you would call reflect.Value.IsValid is
after calling a Value method like Elem, FieldByName, MapIndex, etc.,
that is defined to return the zero Value in certain cases.

Ian

Marvin Renich

unread,
Feb 2, 2015, 8:16:12 PM2/2/15
to golan...@googlegroups.com
* fab...@gmx.de <fab...@gmx.de> [150202 14:56]:
> Thanks for your answers!!! But I still do not understand properly.
>
> When do I have to check if isValid? Is there a common use case, or why
> isn't it enough to compare with nil?
>
> Sorry for my stupid questions.

If you are just learning go, it is likely that you don't want to use the
reflect package yet unless you really know you need to (i.e. you have
learned enough about go to realize that reflect will solve a particular
problem that you can't solve easily without it).

As others have pointed out, IsValid() is implemented on a reflect.Value
struct, not on all interfaces nor on any standard Go types. If you
posted some code that you are confused about, it would help other list
readers to give you some more concrete answers.

...Marvin

fab...@gmx.de

unread,
Feb 3, 2015, 3:53:58 AM2/3/15
to golan...@googlegroups.com, mr...@renich.org
Ok, I think I understand it.


If my program want an element (for example in a struct) that doesn't exists, then for example FieldByName returns "reflect.Value{}". So after FieldByName I have to check if the return value is a valid reflect.Value or not (with IsValid), right? 

Thanks again for your help.

Marvin Renich

unread,
Feb 3, 2015, 8:47:22 AM2/3/15
to golan...@googlegroups.com
* fab...@gmx.de <fab...@gmx.de> [150203 03:54]:
> Ok, I think I understand it.
>
> https://play.golang.org/p/K_fDUIlnr1
>
> If my program want an element (for example in a struct) that doesn't
> exists, then for example FieldByName returns "reflect.Value{}". So after
> FieldByName I have to check if the return value is a valid reflect.Value or
> not (with IsValid), right?

Correct. However, most of the time, you will know at compile time what
structs and fields you want to use, so there will be no need to use the
reflect package at all.

...Marvin

Reply all
Reply to author
Forward
0 new messages