emptygif
Language: English

All languages and frameworks

fasthttp Go

A Go snippet for fasthttp that answers requests for /pixel.gif with the empty GIF, decoded once at startup.

fasthttp Go

Install
go get github.com/valyala/fasthttp
File
main.go
Run
go run .
package main

import (
	"encoding/base64"
	"log"

	"github.com/valyala/fasthttp"
)

var gif, _ = base64.StdEncoding.DecodeString("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")

func handler(ctx *fasthttp.RequestCtx) {
	if !ctx.IsGet() || string(ctx.Path()) != "/pixel.gif" {
		ctx.NotFound()
		return
	}
	ctx.SetContentType("image/gif")
	ctx.Response.Header.Set("Cache-Control", "no-store")
	ctx.SetBody(gif)
}

func main() {
	log.Fatal(fasthttp.ListenAndServe(":8080", handler))
}

We ran this snippet and compared the bytes it returned.

Check it

curl -sI http://localhost:8080/pixel.gif

Official site: fasthttp

More in Go