emptygif
语言: 简体中文

所有语言和框架

Go standard library (net/http) Go

一个用于 Go standard library (net/http) 的 Go 代码片段,以空 GIF 响应对 /pixel.gif 的请求,GIF 在启动时只解码一次。

Go standard library (net/http) Go

文件
main.go
运行
go run .
package main

import (
	"encoding/base64"
	"log"
	"net/http"
)

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

func main() {
	// Go 1.22+ method-aware pattern; GET also answers HEAD.
	http.HandleFunc("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) // small bodies get Content-Length automatically
	})
	log.Fatal(http.ListenAndServe(":8080", nil))
}

我们运行了此代码片段,并比对了它返回的字节。

验证

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

官方网站: Go standard library (net/http)

更多 Go 示例