gpt4 book ai didi

go - 不能使用实现接口(interface)的结构

转载 作者:IT王子 更新时间:2023-10-29 01:37:56 26 4
gpt4 key购买 nike

<分区>

代码:

package main

import "fmt"

type implementation struct {
d []int
}

func (impl *implementation) getData() interface{} {
return impl.d
}

type phase struct{}

type data interface {
getData() interface{}
}

func MakeIntDataPhase() *phase {
return &phase{}
}

func (p *phase) run(population []data) []data {
return nil
}

func main() {
var population []implementation
MyPhase := MakeIntDataPhase()
fmt.Println(MyPhase.run(population))

}

在 Playground 上运行以下代码时出现以下错误:prog.go:30:25: cannot use population (type []implementation) as type []data in argument to MyPhase.run

我是 golang 的新手,我不明白为什么会这样?

结构实现data 接口(interface)实现方法getData()。在 run 方法中使用一段 implementation 还不够吗?

我的推理哪里错了?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com