duai0935 2016-10-19 21:55
浏览 42
已采纳

了解gometalinter取消转换不必要的转换警告

warning: unnecessary conversion (unconvert)

This comes from the following line:

offsetY += 60 + image.Image(*img.Bitmap).Bounds().Max.Y

It took me a while to understand how to convert this interface pointer to an interface yet I do not think that this is the right solution since gometalinter raises a warning.

I want to get the width of the img. img is of struct Image and has a Bitmap pointer to a real image.Image (image stdlib). If I want to call Bounds on the actual image.Image I need to transform the pointer to the interface into an interface.

How should that be done in a more go friendly way?

I have the following go code:

import (
    "image"
    "image/color"
    "image/draw"
)
type Image struct {
    Src    string
    Title  string
    Width  int
    Height int
    Index  int
    Bitmap *image.Image
}

type Images []Image

offsetY = 10
func ComposeImage(imgs Images) image.Image {
    masterRGBAImg := image.NewRGBA(image.Rect(0, 0, 300, 300))
    masterBounds := masterRGBAImg.Bounds()

    for _, img := range imgs {
        draw.Draw(masterRGBAImg, masterBounds,
            *img.Bitmap, image.Point{X: -10, Y: -offsetY + 10}, draw.Src)


        addLabel(masterRGBAImg, 10, offsetY-30, img.Title)

        // HERE ======
        offsetY += 60 + image.Image(*img.Bitmap).Bounds().Max.Y
        // END ======

    }
    return masterRGBAImg
}

// Draw label on image.
func addLabel(img *image.RGBA, x int, y int, label string) {
    col := color.RGBA{50, 50, 50, 255}
    point := fixed.Point26_6{X: fixed.Int26_6(x * 64), Y: fixed.Int26_6(y * 64)}

    d := &font.Drawer{
        Dst:  img,
        Src:  image.NewUniform(col),
        Face: inconsolata.Bold8x16,
        Dot:  point,
    }
    d.DrawString(label)
}
  • 写回答

1条回答 默认 最新

  • dounanyin3179 2016-10-19 22:01
    关注

    First, the bitmap field is a *image.Image, so it's of the same type and you don't need to convert it when you could just dereference it.

    (*img.Bitmap).Bounds()
    

    However, image.Image is an interface. A pointer to an interface is almost always a programming error.

    Change your struct definition to

    type Image struct {
        Src    string
        Title  string
        Width  int
        Height int
        Index  int
        Bitmap image.Image
    }
    

    and you can then just call img.Bitmap.Bounds() directly

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

报告相同问题?

悬赏问题

  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云