Display gif image with webserver in Go (golang)

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

Display gif image with webserver in Go (golang)

问题

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

我感谢您的帮助。


  1. package main
  2. import (
  3. "net/http"
  4. "io"
  5. "encoding/base64"
  6. )
  7. const base64GifPixel = "R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs="
  8. func respHandler(res http.ResponseWriter, req *http.Request) {
  9. res.Header().Set("Content-Type","image/gif")
  10. output,_ := base64.StdEncoding.DecodeString(base64GifPixel)
  11. io.WriteString(res,string(output))
  12. }
  13. func main() {
  14. http.HandleFunc("/", respHandler)
  15. http.ListenAndServe(":8086", nil)
  16. }

我希望这可以帮到您。

英文:

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.


  1. package main
  2. import (
  3. "net/http"
  4. "io"
  5. "encoding/base64"
  6. )
  7. const base64GifPixel = "R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs="
  8. func respHandler(res http.ResponseWriter, req *http.Request) {
  9. res.Header().Set("Content-Type","image/gif")
  10. output,_ := base64.StdEncoding.DecodeString(base64GifPixel)
  11. io.WriteString(res,string(output))
  12. }
  13. func main() {
  14. http.HandleFunc("/", respHandler)
  15. http.ListenAndServe(":8086", nil)
  16. }

答案1

得分: 8

这里似乎工作正常:

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

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

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

英文:

Seems to be working fine here:

  1. $ wget -q -O file.gif http://localhost:8086
  2. $ file file.gif
  3. 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

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

  1. 对我也适用。顺便说一下,如果你正在使用这段代码作为一个信标/跟踪像素的一部分,你可以简单地返回一个204无内容的响应(它比GIF35字节,并且可以实现完全相同的目的):
  2. func EventTracker(w http.ResponseWriter, r *http.Request) {
  3. if r.Method != "GET" {
  4. http.NotFound(w, r)
  5. return
  6. }
  7. // 在这里插入跟踪逻辑
  8. w.WriteHeader(http.StatusNoContent)
  9. }
英文:

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):

  1. func EventTracker(w http.ResponseWriter, r *http.Request) {
  2. if r.Method != "GET" {
  3. http.NotFound(w, r)
  4. return
  5. }
  6. //insert tracking logic here
  7. w.WriteHeader(http.StatusNoContent)
  8. }

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:

确定