简体   繁体   English

为什么不能用指针接收器实现接口

[英]Why can't the interface be implemented with pointer receivers

I'm confused as to why this fails to compile with: 我很困惑为什么这不能编译:

impossible type assertion: Faz does not implement Foo (Bar method has pointer receiver) 不可能的类型断言:Faz没有实现Foo(Bar方法有指针接收器)

if I make the receiver for Faz.Bar a Faz value rather than a Faz pointer then it compiles fine, but I thought it was always better to have pointer receivers so values aren't being copied around? 如果我为Faz.Bar制作Faz.Bar的接收器而不是Faz指针,那么它编译得很好,但我认为拥有指针接收器总是更好,所以值不被复制?

package main

import (
    "log"
)

func main() {
    foo := New().(Faz)
    log.Println(foo)
}

type Foo interface {
    Bar() string
}

func New() Foo {
    return &Faz{}
}

type Faz struct {
}

func (f *Faz) Bar() string {
    return `Bar`
}

Because it's *Faz not Faz . 因为它是*Faz而不是Faz

func main() {
    foo := New().(*Faz)
    log.Println(foo)
}

I think the answer to this question needs to a more retrospective approach towards the grammar, and how would implement it through software engineering. 我认为这个问题的答案需要采用更加回顾性的语法方法,以及如何通过软件工程实现它。 (Excuse the over simplification) (借口过度简化)


First a quick flashback of what are types ? 首先是什么types的快速闪回?
They are just memory blocks with compiler logic on top. 它们只是顶部带有编译器逻辑的内存块。 What makes an array different from a string is what the compiler allows us to do with those memory blocks. 使arraystring不同的是编译器允许我们对这些内存块执行的操作。 (Think deeper and you may begin to realize the true difference between strongly typed and dynamically typed languages.) (深入思考,你可能会开始意识到strongly typeddynamically typed语言之间的真正区别。)

Now next you need to realize that pointers are their own types per say. 接下来你需要意识到每个说法指针都是他们自己的类型。
*variable is a different memory block (aka type) than variable . *variable是不同的存储器块(又名型)比variable It's just that the compiler always assumes that content of *variable is always going to be an address to a memory block of type to the right of the declaration along with other restriction/features it imposes. 只是编译器始终假定*variable内容始终是声明右侧类型的内存块的地址以及它所强加的其他限制/功能。

Then let's recap what an interface is. 然后让我们回顾一下界面是什么。
Pseudo-scientific definition : A set of requirements for any first class citizen to be of a specific type. 伪科学定义 :任何一等公民属于特定类型的一组要求。 Translated to software engineering- any block of memory (types) that has the same memory structure (think back to structure packing ) associated to it as described in a contract ( interface ) can be passed around as with the type name that the contract mentions. 转换为软件工程 - 与合同( interface )中描述的具有相同内存结构(回想到结构打包 )的任何内存(类型)块都可以像合同提到的类型名称一样传递。


Now you may begin to realize that when you say 现在你可能会开始意识到,当你说

func (f *Faz) Bar() string is f 's block of memory holding a function, where f 's type is a pointer to Faz func (f *Faz) Bar() stringf的内存块,包含一个函数,其中f的类型是指向Faz的指针

where areas 哪个地区

func (f Faz) Bar() string is f 's block of memory, where f 's type is Faz func (f Faz) Bar() stringf的内存块,其中f的类型是Faz

So when you are saying that a variable of *Faz type is satisfying a contract, then how can you assume that a variable of Faz type will qualify as interface type in the code? 因此,当您说*Faz类型的变量满足合约时,您如何假设Faz类型的变量将符合代码中的接口类型? Chose who satisfies your contract, and only that type can assume the interface type in your code. 选择满足您的合同,只有该类型可以在您的代码中采用接口类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM