Display gif image with webserver in Go (golang)

huangapple go评论80阅读模式
英文:

Display gif image with webserver in Go (golang)

问题

我正在尝试使用这个简单的Go程序输出一个1x1的透明GIF图像(在base64中预先生成),但似乎无法使其工作。有人知道如何使用预先生成的base64字符串或从磁盘上的文件来实现这个吗?

我感谢您的帮助。


package main

import (
       "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)
}

我希望这可以帮到您。

英文:

I'm trying to output a 1x1 transparent GIF image (pre-generated in base64) with this simple Go program, although I can't seem to get it working. Does anyone have idea on how to do this either with the pre-generated base64 string or with a file from disk?

I appreciate the help.


package main

import (
       "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)
}

答案1

得分: 8

这里似乎工作正常:

$ wget -q -O file.gif http://localhost:8086
$ file file.gif
file.gif: GIF 图像数据, 版本 89a, 1 x 1

你是如何验证它不工作的?如果你用浏览器访问它,我猜它会显示一个空白页面,其中有一个透明像素,这有点难以察觉。 Display gif image with webserver in Go (golang)

另外,强烈建议检查错误,即使在示例代码中也是如此(很多时候示例代码本身就解释了问题)。

英文:

Seems to be working fine here:

$ wget -q -O file.gif http://localhost:8086
$ file file.gif
file.gif: GIF image data, version 89a, 1 x 1

How are you verifying that it is not working? If you access it with a web browser, I suppose it'll show an empty page with a transparent pixel in it, which is a bit hard to spot. Display gif image with webserver in Go (golang)

As a side note, checking errors is strongly recommended, even in sample code (many times the sample code explains itself).

答案2

得分: 1

这段代码的中文翻译如下:

对我也适用。顺便说一下,如果你正在使用这段代码作为一个信标/跟踪像素的一部分,你可以简单地返回一个204无内容的响应(它比GIF小35字节,并且可以实现完全相同的目的):

    func EventTracker(w http.ResponseWriter, r *http.Request) {
        if r.Method != "GET" {
            http.NotFound(w, r)
            return
        }
        // 在这里插入跟踪逻辑
        w.WriteHeader(http.StatusNoContent)
    }
英文:

works for me too. by the way, if you are doing this as a part of a beacon/tracking pixel, you could simply return a 204 no content (it's 35 bytes smaller than the gif and it can serve the exact same purpose):

func EventTracker(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.NotFound(w, r)
		return
	}
    //insert tracking logic here
	w.WriteHeader(http.StatusNoContent)
}

huangapple
  • 本文由 发表于 2013年9月12日 21:05:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/18765171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定