Golang Iris Web - 提供 1x1 像素

Golang Iris Web - Serve 1x1 pixel

我是 Golang 新手,正在尝试使用名为 iris 的框架。

我的问题是如何提供 1x1 gif 像素,而不是使用 c.HTML 上下文,而是浏览器标题变成 1x1 图像 gif 的方式。该图像将用于跟踪。

任何帮助将不胜感激。

注意:我对 iris 不是很熟悉,所以这个解决方案可能不符合地道。

package main

import (
    "github.com/kataras/iris"
    "image"    
    "image/color"
    "image/gif"
)

func main() {

    iris.Get("/", func(ctx *iris.Context) {
        img := image.NewRGBA(image.Rect(0, 0, 1, 1)) //We create a new image of size 1x1 pixels
        img.Set(0, 0, color.RGBA{255, 0, 0, 255}) //set the first and only pixel to some color, in this case red

        err := gif.Encode(ctx.Response.BodyWriter(), img, nil) //encode the rgba image to gif, using gif.encode and write it to the response
        if err != nil {
            panic(err) //if we encounter some problems panic
        }
        ctx.SetContentType("image/gif") //set the content type so the browser can identify that our response is actually an gif image
    })

    iris.Listen(":8080")
}

更好理解的链接: