emptygif
語言: 繁體中文

所有語言和框架

fasthttp Go

適用於 fasthttp 的 Go 程式碼片段,以空 GIF 回應對 /pixel.gif 的請求,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 範例