i use golang study golang, Is there something wrong with my goland? Need to change configuration information? my code
package main
import (
"golang.org/x/tour/pic"
"image"
"image/color"
)
type Image struct{} //新建一个Image结构体
func (i Image) ColorModel() color.Model { //实现Image包中颜色模式的方法
return color.RGBAModel
}
func (i Image) Bounds() image.Rectangle { //实现Image包中生成图片边界的方法
return image.Rect(0, 0, 200, 200)
}
func (i Image) At(x, y int) color.Color { //实现Image包中生成图像某个点的方法
return color.RGBA{uint8(x), uint8(y), uint8(255), uint8(255)}
}
func main() {
m := Image{}
pic.ShowImage(m) //调用
}
CodePudding user response:
There is nothing wrong with your code or your Goland.
The reason why the image is displayed on the Go Playground is because the Go Playground itself supports this. It's kind of like a feature.
pic.ShowImage()
does nothing magical just prints text to the standard output. It prints IMAGE:
and the Base64-encoded data of the image you pass, encoded as a PNG image. It's the Go Playground that interprets the standard output, and if the output starts with IMAGE:
, the backend interprets the rest of the output as the Base64 encoded form of the image, generates an HTML <img>
tag displaying that image.
See this example to verify:
func main() {
img := image.NewRGBA(image.Rect(0, 0, 100, 100))
red := color.RGBA{R: 255, A: 255}
for i := 0; i < 100; i {
img.Set(i, i, red)
}
buf := &bytes.Buffer{}
if err := png.Encode(buf, img); err != nil {
panic(err)
}
fmt.Println("IMAGE:" base64.StdEncoding.EncodeToString(buf.Bytes()))
}
This code running on the Go Playground will result in (try it: