Re: [go-nuts] Why get an element from []interface{} `arr[0] == nil` is not true?

2017-02-16 Thread Dragos Harabor
I understand that, I was just curious to see if there was another way to do 
it besides reflection.

On Thursday, February 16, 2017 at 12:03:43 PM UTC-8, Axel Wagner wrote:
>
> The question is: Why would you want to know? nil-pointers are perfectly 
> fine implementations of an interface. By caring whether the value 
> implementing your interface is a nil-pointer, you are breaking the 
> abstraction of your interface.
>
> On Thu, Feb 16, 2017 at 8:37 PM, Dragos Harabor  > wrote:
>
>> Actually it doesn't do what I asked, sorry if I was not clear, I was 
>> expecting your example to print "true true", got excited too quickly.
>>
>> https://play.golang.org/p/WnDyT9OOpr
>>
>>
>> On Thursday, February 16, 2017 at 11:32:59 AM UTC-8, Dragos Harabor wrote:
>>>
>>> Nice, thank you!
>>>
>>> On Thursday, February 16, 2017 at 11:25:31 AM UTC-8, Jan Mercl wrote:

 On Thu, Feb 16, 2017 at 8:20 PM Dragos Harabor  
 wrote:

 > Is there any way to check whether the value stored in a non-nil 
 interface is nil when you don't know the type, short of using 
 reflect.Value.IsNil()?

 For example: https://play.golang.org/p/Sdqx7Frjxj

 -- 

 -j

>>> -- 
>> 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.
>>
>
>

-- 
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.


Re: [go-nuts] Why get an element from []interface{} `arr[0] == nil` is not true?

2017-02-16 Thread 'Axel Wagner' via golang-nuts
The question is: Why would you want to know? nil-pointers are perfectly
fine implementations of an interface. By caring whether the value
implementing your interface is a nil-pointer, you are breaking the
abstraction of your interface.

On Thu, Feb 16, 2017 at 8:37 PM, Dragos Harabor  wrote:

> Actually it doesn't do what I asked, sorry if I was not clear, I was
> expecting your example to print "true true", got excited too quickly.
>
> https://play.golang.org/p/WnDyT9OOpr
>
>
> On Thursday, February 16, 2017 at 11:32:59 AM UTC-8, Dragos Harabor wrote:
>>
>> Nice, thank you!
>>
>> On Thursday, February 16, 2017 at 11:25:31 AM UTC-8, Jan Mercl wrote:
>>>
>>> On Thu, Feb 16, 2017 at 8:20 PM Dragos Harabor 
>>> wrote:
>>>
>>> > Is there any way to check whether the value stored in a non-nil
>>> interface is nil when you don't know the type, short of using
>>> reflect.Value.IsNil()?
>>>
>>> For example: https://play.golang.org/p/Sdqx7Frjxj
>>>
>>> --
>>>
>>> -j
>>>
>> --
> 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.
>

-- 
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.


Re: [go-nuts] Why get an element from []interface{} `arr[0] == nil` is not true?

2017-02-16 Thread Dragos Harabor
Actually it doesn't do what I asked, sorry if I was not clear, I was 
expecting your example to print "true true", got excited too quickly.

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


On Thursday, February 16, 2017 at 11:32:59 AM UTC-8, Dragos Harabor wrote:
>
> Nice, thank you!
>
> On Thursday, February 16, 2017 at 11:25:31 AM UTC-8, Jan Mercl wrote:
>>
>> On Thu, Feb 16, 2017 at 8:20 PM Dragos Harabor  wrote:
>>
>> > Is there any way to check whether the value stored in a non-nil 
>> interface is nil when you don't know the type, short of using 
>> reflect.Value.IsNil()?
>>
>> For example: https://play.golang.org/p/Sdqx7Frjxj
>>
>> -- 
>>
>> -j
>>
>

-- 
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.


Re: [go-nuts] Why get an element from []interface{} `arr[0] == nil` is not true?

2017-02-16 Thread Dragos Harabor
Nice, thank you!

On Thursday, February 16, 2017 at 11:25:31 AM UTC-8, Jan Mercl wrote:
>
> On Thu, Feb 16, 2017 at 8:20 PM Dragos Harabor  > wrote:
>
> > Is there any way to check whether the value stored in a non-nil 
> interface is nil when you don't know the type, short of using 
> reflect.Value.IsNil()?
>
> For example: https://play.golang.org/p/Sdqx7Frjxj
>
> -- 
>
> -j
>

-- 
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.


Re: [go-nuts] Why get an element from []interface{} `arr[0] == nil` is not true?

2017-02-16 Thread Jan Mercl
On Thu, Feb 16, 2017 at 8:20 PM Dragos Harabor  wrote:

> Is there any way to check whether the value stored in a non-nil interface
is nil when you don't know the type, short of using reflect.Value.IsNil()?

For example: https://play.golang.org/p/Sdqx7Frjxj

-- 

-j

-- 
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.


Re: [go-nuts] Why get an element from []interface{} `arr[0] == nil` is not true?

2017-02-16 Thread Dragos Harabor
Is there any way to check whether the value stored in a non-nil interface 
is nil when you don't know the type, short of using reflect.Value.IsNil()?

Thanks.

On Wednesday, February 15, 2017 at 10:11:55 AM UTC-8, rog wrote:
>
> In addition to what others have said, note that the 
> comparison works as you expected if you use a typed nil: 
>
> https://play.golang.org/p/jA57mTKbxa 
>
>
> On 15 February 2017 at 08:40, Felix Sun  
> wrote: 
> > https://play.golang.org/p/qYA8Ddnnye 
> > 
> > ``` 
> > 
> > package main 
> > 
> > import ( 
> > "fmt" 
> > ) 
> > 
> > type Obj struct { 
> > One *Obj 
> > } 
> > 
> > func main() { 
> > var o = {} 
> > 
> > var arr = []interface{}{o.One} 
> > 
> > fmt.Println(arr[0]) 
> > fmt.Println("why this is not true?", arr[0] == nil) 
> > } 
> > 
> > 
> > ``` 
> > 
> > 
> > Why `arr[0] == nil` is not true? 
> > 
> > -- 
> > 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. 
>

-- 
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.


Re: [go-nuts] Why get an element from []interface{} `arr[0] == nil` is not true?

2017-02-15 Thread roger peppe
In addition to what others have said, note that the
comparison works as you expected if you use a typed nil:

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


On 15 February 2017 at 08:40, Felix Sun  wrote:
> https://play.golang.org/p/qYA8Ddnnye
>
> ```
>
> package main
>
> import (
> "fmt"
> )
>
> type Obj struct {
> One *Obj
> }
>
> func main() {
> var o = {}
>
> var arr = []interface{}{o.One}
>
> fmt.Println(arr[0])
> fmt.Println("why this is not true?", arr[0] == nil)
> }
>
>
> ```
>
>
> Why `arr[0] == nil` is not true?
>
> --
> 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.

-- 
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.


Re: [go-nuts] Why get an element from []interface{} `arr[0] == nil` is not true?

2017-02-15 Thread Jan Mercl
On Wed, Feb 15, 2017 at 9:40 AM Felix Sun  wrote:

Why `arr[0] == nil` is not true?

Because it's not a nil interface{}: https://play.golang.org/p/w-od_KHWlc

See also https://golang.org/doc/faq#nil_error

-- 

-j

-- 
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.


Re: [go-nuts] Why get an element from []interface{} `arr[0] == nil` is not true?

2017-02-15 Thread 'Axel Wagner' via golang-nuts
This is a frequently asked question  (it
has nothing to do with the slice).

On Wed, Feb 15, 2017 at 9:40 AM, Felix Sun  wrote:

> https://play.golang.org/p/qYA8Ddnnye
>
> ```
>
> package main
>
> import (
> "fmt"
> )
>
> type Obj struct {
> One *Obj
> }
>
> func main() {
> var o = {}
>
> var arr = []interface{}{o.One}
>
> fmt.Println(arr[0])
> fmt.Println("why this is not true?", arr[0] == nil)
> }
>
>
> ```
>
>
> Why `arr[0] == nil` is not true?
>
> --
> 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.
>

-- 
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.


[go-nuts] Why get an element from []interface{} `arr[0] == nil` is not true?

2017-02-15 Thread Felix Sun
https://play.golang.org/p/qYA8Ddnnye

```

package main

import (
"fmt"
)

type Obj struct {
One *Obj
}

func main() {
var o = {}

var arr = []interface{}{o.One}

fmt.Println(arr[0])
fmt.Println("why this is not true?", arr[0] == nil)
}


```


Why `arr[0] == nil` is not true?

-- 
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.