1 回答

TA貢獻1818條經驗 獲得超8個贊
您的代碼或您的 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 ://go.dev/play/p/N0RQSTBcqdE ):
- 1 回答
- 0 關注
- 524 瀏覽
添加回答
舉報