doubt about reflect.Type.String and empty interface

89 views
Skip to first unread message

Manlio Perillo

unread,
Jan 5, 2020, 11:22:51 AM1/5/20
to golang-nuts
Hello and happy new year.

I'm re-reading the Go Language Specification, and at https://golang.org/ref/spec#Variables I found this example:

  var x interface{} // x is nil and has static type interface{}
  var v *T               // v has value nil, static type *T
  x = 42                 // x has value 42 and dynamic type int
  x = v                   // x has value (*T)(nil) and dynamic type *T

Since the first comment is different for the others (`x is nil` against `x has value`) I decided to run a simple test
https://play.golang.org/p/QAX92NQDqO4.  It has an additional entry:
  var z *interface{}


The output of the first and last line is

x is nil and has static type interface{}
<invalid reflect.Value> <nil>

z is nil and has static type *interface{}
<nil> *interface {}


The question is: why reflect.Type.String() returns `<nil>` instead of  `interface{}` ?
Is the example printing the static or the dynamic type?


Thanks
Manlio Perillo

Ian Lance Taylor

unread,
Jan 5, 2020, 11:29:41 AM1/5/20
to Manlio Perillo, golang-nuts
https://blog.golang.org/laws-of-reflection

The reflect package takes value of type interface{}, so passing a
value of type interface{} and passing a value of type *interface{}
acts differently.

Ian

Manlio Perillo

unread,
Jan 6, 2020, 6:57:45 PM1/6/20
to Ian Lance Taylor, golang-nuts
On Sun, Jan 5, 2020 at 5:29 PM Ian Lance Taylor <ia...@golang.org> wrote:
>
> On Sun, Jan 5, 2020 at 8:23 AM Manlio Perillo <manlio....@gmail.com> wrote:
> >
>> [...]
> > I decided to run a simple test
> > https://play.golang.org/p/QAX92NQDqO4.
> >
> > [...]
> >
> > The question is: why reflect.Type.String() returns `<nil>` instead of `interface{}` ?
> > Is the example printing the static or the dynamic type?
>
> https://blog.golang.org/laws-of-reflection
>
> The reflect package takes value of type interface{}, so passing a
> value of type interface{} and passing a value of type *interface{}
> acts differently.
>
> Ian

Right, thanks. I was probably assuming TypeOf to be a generic function.

I wrote another test https://play.golang.org/p/hoTAnijCfg1.
Now it is clear why the first entry can only print nil for the type
and <invalid reflect.Value> for the value.

However printf %v verb prints nil for both an empty interface and an
interface with a dynamic value of nil:
https://play.golang.org/p/m_WfR2SPWQ3
Can this cause confusion?

Thanks
Manlio Perillo

Marvin Renich

unread,
Jan 7, 2020, 7:09:30 AM1/7/20
to golang-nuts
* Manlio Perillo <manlio....@gmail.com> [200106 18:57]:
> I wrote another test https://play.golang.org/p/hoTAnijCfg1.
> Now it is clear why the first entry can only print nil for the type
> and <invalid reflect.Value> for the value.
>
> However printf %v verb prints nil for both an empty interface and an
> interface with a dynamic value of nil:
> https://play.golang.org/p/m_WfR2SPWQ3
> Can this cause confusion?

Yes, it definitely can (and probably does). However, if you change "%v"
to "%#v", it helps to clarify things:

https://play.golang.org/p/K4nPb27Btok

...Marvin

Reply all
Reply to author
Forward
0 new messages