emptygif
Dil: Türkçe

Tüm diller ve framework'ler

Go standard library (net/http) Go

Go standard library (net/http) için, /pixel.gif isteklerine başlangıçta bir kez çözülen boş GIF ile yanıt veren bir Go örneği.

Go standard library (net/http) Go

Dosya
main.go
Çalıştır
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))
}

Bu örneği çalıştırıp döndürdüğü baytları karşılaştırdık.

Kontrol et

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

Resmî site: Go standard library (net/http)

Go için daha fazlası