dttnb997315 2016-10-11 14:43
浏览 75
已采纳

golang练习:方法中缺少图像

I am wokring through the exercise:images at https://tour.golang.org/methods/25 and I have run into a problem. Here is my code...

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)
}

it gives me the error...

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)

But image.NewRGBA returns a *NRGBA and that does indeed have an At() method. Also I assume since the At() method is the last method required by the image.Image interface then it is finding the other two required methods...so what is up with the At()?

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

image.Image interface: https://golang.org/pkg/image/#Image

  • 写回答

1条回答 默认 最新

  • donglian2106 2016-10-11 15:07
    关注

    Your type Image does not implement the At method. If you want your type to inherit the methods implemented by *image.RGBA, use an anonymous field:

    type Image struct{
         *image.RGBA   
    }
    

    See https://golang.org/doc/effective_go.html#embedding.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!