GoLang上的接口函数调用

I am having a problem go interfaces.

Here is my main.go file;

package main

import (
    "fmt"
    "bitbucket.org/xyz/trash/a"
)

// Second -
type Second interface {
    Area() float64
}

// Area -
func Area() float64 {
    return 2
}


func main() {

    r := new(a.Rect)

    n := new(Second)

    r.F = *n

    fmt.Println(r.Area()) 

}

And my other package, a.go;

package a

// First -
type First interface {
    Area() float64
}

// Rect -
type Rect struct {
    F First 
}

// Area -
func (r Rect) Area() float64 {
    return 1
}

I am expecting this line

fmt.Println(r.Area())

to print "2", not "1". What am i missing?

Thanks for your help.

First, look at your code again. As written, a.Rect.Area() returns 1. It always returns 1. Set all the properties you like. It returns 1. Gnash your teeth and curse the gods. It returns 1. Because it's a function that returns 1. That's what it does.

Write it like this:

// Area -
func (r Rect) Area() float64 {
    return r.F.Area()
}

...and you'll panic because you've got another issue.

Area() in main.go doesn't have a receiver. It has nothing to do with interfaces. It's just a function. No objects. No implementing anything. No polymorphism. No nothing.

You've declared n as a pointer to an interface, not an implementation of an interface. It's nil. If you try to dereference it the whole world will come crashing down into the void.

If you want to be able to call n.Area() then make Second a concrete type.

(In main.go)

// Second -
type Second struct {}

// Area -
func (s Second) Area() float64 {
    return 2
}

Do you see the difference? Second is now a struct that implements the interface a.First because it is the receiver of an appropriate Area() method. Thus you will get the expected behavior.

There is a misunderstanding of what interfaces are. In go, a type implements an interface if it implements the function defined for the interface. So, in your code, interfaces a.First and main.Second are equivalent. A type implements both of them or none at all.

So, the type Rect is both a a.First and a main.Second. The field F inside Rect means that it contains another type that implements First (or Second because they are the same).

When you call r.Area(), go executes the function func (r Rect) Area() uint64. With the line r.F = *n, you assign the interface Second to First (they are equivalent, no problem). However, if you try to call r.F.Area() it will panic because F is not a type that implements First, it is First.

So, you should create another type which implements First then assign it to r. Then you can call r.F.Area().