Ah, thanks, surprised I've never hit this issue before.

So, for purely academic reasons, what if I wanted to write a function to 
look inside an interface value to determine if its value pointer is nil?

I tried this, following from your simplified example Steven.... 
https://play.golang.org/p/oG2aQzlKfc

var m map[string]interface{}
var i interface{} = m                    // i is interface{}, right?
id := reflect.ValueOf(i).InterfaceData() // nope, panics "call of 
reflect.Value.InterfaceData on map Value"




On Monday, 8 August 2016 19:05:53 UTC+1, Steven Blenkinsop wrote:
>
> Perhaps for clarity, the trip through reflect is a red herring. All you 
> need is to put the map in an interface:
>
> package main
>>
>> import (
>> "fmt"
>> )
>>
>> func main() {
>> var m map[string]interface{}
>> if m != nil {
>> panic("m != nil")
>> }
>> var i interface{} = m
>> if i == nil {
>> // I expect this.
>> fmt.Println("OK")
>> } else {
>> // This is what happens.
>> fmt.Printf("nil != %# v\n", i)
>> }
>> // output:
>> // nil != map[string]interface {}(nil)
>> }
>>
>
> The reason the interface is non-nil in this case is described in Ian's 
> link.
>
> On Mon, Aug 8, 2016 at 1:27 PM Ian Lance Taylor <ia...@golang.org 
> <javascript:>> wrote:
>
>> On Mon, Aug 8, 2016 at 10:07 AM, Sam Salisbury <samsal...@gmail.com 
>> <javascript:>> wrote:
>> > The code speaks for itself, I thought I was understanding reflection up
>> > until this point... It seems that the zero value of maps is nil, until 
>> you
>> > round-trip them to a reflect.Value, and they become non-nil.
>>
>> This is an instance of https://golang.org/doc/faq#nil_error .
>>
>> Ian
>>
>> --
>> 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 <javascript:>.
>> 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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to