chi Go
適用於 chi 的 Go 程式碼片段,以空 GIF 回應對 /pixel.gif 的請求,GIF 在啟動時只解碼一次。
chi Go
package main
import (
"encoding/base64"
"log"
"net/http"
"github.com/go-chi/chi/v5"
)
var gif, _ = base64.StdEncoding.DecodeString("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")
func main() {
r := chi.NewRouter()
r.Get("/pixel.gif", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/gif")
w.Header().Set("Cache-Control", "no-store")
w.Write(gif)
})
log.Fatal(http.ListenAndServe(":8080", r))
}
我們執行了這段程式碼,並比對了它回傳的位元組。
驗證
curl -sI http://localhost:8080/pixel.gif