Golang Iris Web - Служит 1x1 пикселей
Я новичок в Golang и пытаюсь использовать фреймворк под названием iris.
Моя проблема заключается в том, как обслуживать пиксели 1x1 gif, не используя контекст c.HTML, а в том, как заголовок браузера становится изображением 1x1 gif. Изображение будет использоваться для отслеживания.
Любая помощь будет высоко оценена.
1 ответ
Решение
Внимание: я не очень знаком с радужной оболочкой, поэтому решение может быть не идиоматическим.
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")
}
Ссылки для лучшего понимания: