我用golang學習golang,我的goland有問題嗎?需要更改配置資訊?我的代碼
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) //呼叫
}

我使用
uj5u.com熱心網友回復:
您的代碼或 Goland 沒有任何問題。
之所以在 Go Playground 上顯示影像,是因為 Go Playground 本身支持這一點。這有點像一個功能。
pic.ShowImage()沒有什么神奇的,只是將文本列印到標準輸出。它列印IMAGE:您傳遞的影像的 Base64 編碼資料,編碼為 PNG 影像。Go Playground 解釋標準輸出,如果輸出以 開頭IMAGE:,則后端將輸出的其余部分解釋為影像的 Base64 編碼形式,生成<img>顯示該影像的 HTML 標記。
請參閱此示例以驗證:
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()))
}
在 Go Playground 上運行的這段代碼將導致(試試看:
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/514597.html
標籤:图片去去游乐场
上一篇:為什么reflect.TypeOf(new(Encoder)).Elem()!=reflect.TypeOf(interfaceVariable)?
