Hopefully these points help to clarify things:

   - The types of the parameters to compare of the shout interface type
   - The comparison operator == therefore operates on two interface values.
   With reference to the spec (
   https://golang.org/ref/spec#Comparison_operators):

Interface values are comparable. Two interface values are equal if they
> have identical dynamic types and equal dynamic values or if both have value
> nil


The dynamic types in all instances in your example are *a; the dynamic
values (i.e. the *a values) are all non-nil, but only in the third case are
the values (i.e. the address values) equal

   - For both &a and &b, you are using the address operator (
   https://golang.org/ref/spec#Address_operators). The type of the
   variables (parameters) a and b is the shout interface type. Hence the
   expressions &a and &b will both be of type *shout. The value returned by &a
   is the address of the variable (parameter) a. Similarly for b. These values
   (of type *shout) do not have to be equal for the comparison a == b to
   return true; the equality operator on interface values is defined in a
   specific way, different to the comparison of pointers (which is in effect
   what you're doing by visually comparing &a and &b)


Paul



On 6 January 2017 at 10:24, Ignazio Di Napoli <neclep...@gmail.com> wrote:

> I'm very curious about the answer to this unanswered StackOverflow
> question: http://stackoverflow.com/questions/41357948/what-
> golang-compiler-will-do-when-fmt-println
>
>
> import (
>     "fmt")
>
> type shout interface {
>     echo()}
>
> type a struct {}
> func (*a) echo () {
>     fmt.Println("a")}
>
> type b struct {}
> func (*b) echo () {
>     fmt.Println("b")}
>
> func compare(a, b shout) {
>     //fmt.Println(&a, &b)
>     if a == b {
>         fmt.Println("same")
>     } else {
>         fmt.Println("not same")
>     }}
>
> func main() {
>     a1 := &a{}
>     b1 := &b{}
>     a2 := &a{}
>     a1.echo()
>     b1.echo()
>     compare(a1, b1)
>     compare(a1, a2)
>     compare(a1, a1) }
>
>
> The output is different if a fmt.Println is commented or not.
>
> Commented:
>
> not samenot same
> same
>
>
> Not commented:
>
> 0x1040a120 0x1040a128not same0x1040a140 0x1040a148
> same0x1040a158 0x1040a160
> same
>
>
>
> To me, it seems a compiler bug, or some undefined behaviour.
> Can someone help me understand?
>
>
> Thank you.
>
>
>
> --
> 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.

Reply via email to