golang 练习 :images missing at method

标签 go

我正在完成练习:图片位于 https://tour.golang.org/methods/25我遇到了一个问题。这是我的代码...

package main

import (
    "golang.org/x/tour/pic"
    "image"
)

type Image struct{
    image *image.RGBA   
}

func main() {
    rect := image.Rect(0,0,255,255)
    myImage := image.NewRGBA(rect)
    m := Image{myImage}
    pic.ShowImage(m)
}

它给了我错误...

tmp/sandbox089594299/main.go:16: cannot use m (type Image) as type image.Image in argument to pic.ShowImage:
Image does not implement image.Image (missing At method)

但是 image.NewRGBA 返回一个 *NRGBA 并且确实有一个 At() 方法。我还假设 At() 方法是 image.Image 接口(interface)所需的最后一个方法,然后它正在寻找其他两个必需的方法......所以这是怎么回事使用 At()?

图像.NRGBA:https://golang.org/pkg/image/#NRGBA

image.Image接口(interface):https://golang.org/pkg/image/#Image

最佳答案

您的类型 Image 没有实现 At 方法。如果您希望您的类型继承由*image.RGBA 实现的方法,请使用匿名字段:

type Image struct{
     *image.RGBA   
}

参见 https://golang.org/doc/effective_go.html#embedding .

关于golang 练习 :images missing at method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39979956/

相关文章:

time - 使用time.Format时的本地化

go - golang 中的类型不一致,不能将 <Type> 用作 <Type>

go - 通用函数中的Golang眼镜蛇命令标志未从cli获取值

go - proto3 -> 使用自定义扩展,导致在 go 代码中导入包 ("google/protobuf")

mysql - Go MySql 驱动程序没有正确设置时间

http - Go 中 ResolveReference 时如何避免 URL 结尾斜杠被删除

time - 如何在 Go 中检测时间?

go - 使用ssh将带有标志的本地脚本运行到远程计算机

go - Kubernetes(OpenShift)👉kubectl(oc-OpenShift CLI) `patch`返回错误: cannot unmarshal array into Go value of type map[string]interface {}

http - 去 - JSON-RPC - "too many colons"