when I usevar tf intf = &t1, It's correct But when I usevar tf intf = &t1, it's not
Since in your example you are using tf intf = t1 (and it does not compile), I think you probably meant:
when I usevar tf intf = &t1, It's correct But when I usevar tf intf = t1, it's not
So the question here is why &T{"t1"} (pointer) fulfils the interface intf while T{"t1"} (value) does not.
In your example, the interface has two methods M1 and M2.
Type T implements both methods BUT:
- M1 has
T value as the receiver
- M2 has
T pointer as the receiver
So why does the compiler consider a T pointer to have both methods implemented but considers that a T value does not?
Answer can be found here in the spec:
https://golang.org/ref/spec#Method_sets
The method set of any other type T consists of all methods declared with receiver type T. The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T (that is, it also contains the method set of T).
The spec says that a T pointer has all methods defined with receivers T and *T (so both M1 and M2 in this case).
But a value of T only has the methods defined with receiver T (so only M1 in this case).
Since the interface has both methods, only the pointer can fulfil it.