如何比较两个值为结构但作为接口返回的值

I am really new with programming in Go and got stuck here. I have a function A which creates a struct from 3 different values a, b, c.

func A() interface{} {
    s := new(struct{
        a, b, c int
    })
    //fill the values in s
    return s
}

Now when I am testing this function, how will I compare the returned interface with some expected result?

I cannot move the struct definition global

If someone have a better way of doing this type of testing, please suggest.

Although it's possible what you want, this comparison looks really "hackish", you should redesign to not require such comparison.

But let's see how to do it. Let A()'s implementation be like this:

func A() interface{} {
    return &struct {
        a, b, c int
    }{1, 2, 3}
}

If you want to compare the result, you have to first specify the value to compare to. One way is to use an anonymous struct:

got := A()
exp := struct {
    a, b, c int
}{1, 2, 3}

Now since A() returns a pointer (wrapped in an interface{}), you can't really compare that. Comparing pointers compares the pointer value, not the pointed value. So first we should get the pointed value. For that, we may use reflection:

gotNoptr := reflect.ValueOf(got).Elem().Interface()

Now we have 2 struct values, and they are comparable if all their fields are comparable (which is true in this case):

if gotNoptr == exp {
    fmt.Println("Match")
} else {
    fmt.Printf("Mismatch")
}

This will output (try it on the Go Playground):

Match

If you would change a number in the exp value, output would turn into Mismatch.

An alternate way would be to declare exp to be a pointer too:

exp := &struct {
    a, b, c int
}{1, 2, 3}

And use reflect.DeepEqual() to compare the pointed struct values:

if reflect.DeepEqual(got, exp) {
    fmt.Println("Match")
} else {
    fmt.Printf("Mismatch")
}

Try this one on the Go Playground.