emptygif
語言: 繁體中文

所有語言和框架

Encore.go (raw endpoint) Go

適用於 Encore.go (raw endpoint) 的 Go 程式碼片段,以空 GIF 回應對 /pixel.gif 的請求,GIF 在啟動時只解碼一次。

Encore.go (raw endpoint) Go

安裝
encore app create --example=hello-world
檔案
pixel.go
執行
encore run --port 8080
// pixel/pixel.go: a package with an //encore:api endpoint is an Encore service.
package pixel

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

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

// Raw endpoints get the plain http.ResponseWriter.
//
//encore:api public raw method=GET path=/pixel.gif
func Pixel(w http.ResponseWriter, req *http.Request) {
	w.Header().Set("Content-Type", "image/gif")
	w.Header().Set("Cache-Control", "no-store")
	w.Write(gif)
}

我們執行了這段程式碼,並比對了它回傳的位元組。

驗證

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

官方網站: Encore.go (raw endpoint)

更多 Go 範例