emptygif
言語: 日本語

すべての言語・フレームワーク

fasthttp Go

/pixel.gif へのリクエストに空の GIF を返す、fasthttp 用の Go スニペット。GIF は起動時に一度だけデコードします。

fasthttp Go

インストール
go get github.com/valyala/fasthttp
ファイル
main.go
実行
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))
}

このスニペットは実行し、返されたバイト列を照合しました。

確認

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

公式サイト: fasthttp

Go の他の例