我用高朗学习高朗,我的高朗有什么问题吗?需要更改配置信息吗? 我的代码

package main

import (
    "golang.org/x/tour/pic"
    "image"
    "image/color"
)

type Image struct{} //新建一个Image struct 体

func (i Image) ColorModel() color.Model { //实现Image包中 colored颜色 模式的方法
    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) //调用
}

enter image description here

i use https://tour.go-zh.org/methods/24 and run golang return a picture enter image description here

推荐答案

您的代码或您的戈兰德没有任何问题.

图像之所以显示在Goplayground 上,是因为Goplayground 本身支持这一点.这有点像是一种特写.

pic.ShowImage()没有什么神奇之处,只是将文本打印到标准输出.它打印IMAGE:和您传递的图像的Base64编码数据,编码为PNG图像.Go Playround解释标准输出,如果输出以IMAGE:开始,则后端将输出的其余部分解释为图像的Base64编码形式,并生成显示该图像的HTML<img>标记.

请参阅此示例以验证:

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()))
}

在Goplayground 上运行的这段代码将导致(try :https://go.dev/play/p/N0RQSTBcqdE):

enter image description here

Go相关问答推荐

无法在Macos上使用Azure Speech golang SDK

为什么(编码器).EncodeElement忽略";,innerxml";标记?

包裹网.Conn导致挂起读取

go测试10m后如何避免超时

如何在 Chi Router 的受保护路由下提供静态文件(尤其是图像)?

有没有办法让sqlc生成可以使用pgxpool的代码

Kperf 构建失败

如何使用gosdk在Dynamodb中进行UpdateItem时,将ValueBuilder对象声明为StringSet类型?

git ls-remote 成功而 go get 失败

Go struct 匿名字段是公开的还是私有的?

Yocto 无法交叉编译 GoLang Wails 应用程序

go - 仅在函数即将返回错误时清理资源

SSH 代理,数据包长度错误

读取非UTF8编码的文件内容并正确打印出来

golang pic.ShowImage 为什么它不生成图像而是向我发送base64值

将接口方法的参数限制为几个允许的 struct ?

Grafana/Prometheus 将多个 ip 可视化为查询

有没有办法在golang中映射一组对象?

Gin中测试模式有什么用

如果在调用 http.Get(url) 时发生错误,我们是否需要关闭响应对象?