我正在嘗試使用這個(gè)簡單的 Go 程序輸出 1x1 透明 GIF 圖像(在 base64 中預(yù)先生成),盡管我似乎無法讓它工作。有沒有人知道如何使用預(yù)先生成的 base64 字符串或磁盤文件來執(zhí)行此操作?我很感激你的幫助。package mainimport ( "net/http" "io" "encoding/base64")const base64GifPixel = "R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs="func respHandler(res http.ResponseWriter, req *http.Request) { res.Header().Set("Content-Type","image/gif") output,_ := base64.StdEncoding.DecodeString(base64GifPixel) io.WriteString(res,string(output))}func main() { http.HandleFunc("/", respHandler) http.ListenAndServe(":8086", nil)}
2 回答

繁華開滿天機(jī)
TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊
似乎在這里工作正常:
$ wget -q -O file.gif http://localhost:8086
$ file file.gif
file.gif: GIF image data, version 89a, 1 x 1
你如何驗(yàn)證它不起作用?如果您使用網(wǎng)絡(luò)瀏覽器訪問它,我想它會(huì)顯示一個(gè)帶有透明像素的空白頁面,這有點(diǎn)難以發(fā)現(xiàn)。:-)
作為旁注,強(qiáng)烈建議檢查錯(cuò)誤,即使是在示例代碼中(很多時(shí)候示例代碼會(huì)自我解釋)。
- 2 回答
- 0 關(guān)注
- 276 瀏覽
添加回答
舉報(bào)
0/150
提交
取消