emptygif
言語: 日本語

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

Go standard library (net/http) Go

/pixel.gif へのリクエストに空の GIF を返す、Go standard library (net/http) 用の Go スニペット。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 の他の例