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

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

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败