fmt.Printf和log.Println之间的输出不同。

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

%!B(MISSING) different output between fmt.Printf and log.Println

问题

我从json.Marshal返回了一些字节。如果像这样将它们记录到标准输出:

log.Println(string(b))

它们的输出如下:

{"encoded":"%2B"}

如果我使用以下方式将它们写入磁盘:

fmt.Fprintf(w, string(b))

然后像这样查看已写入的文件:

cat 文件名

它们的输出变成了:

{"encoded":"%!B(MISSING)"}

据我所知,string(b)的输出确实是第一个也是我期望的输出。我做错了什么?

英文:

I have some bytes returned from json.Marshal. If log them to stdout like this:

log.Println(string(b))

They are output like this:

{"encoded":"%2B"}

If I write them to disk with

fmt.Fprintf(w, string(b))

And then cat the file they have been written like this:

{"encoded":"%!B(MISSING)"}

As far as I can tell the output of string(b) really is the first, and my expected, output. What am I doing wrong?

答案1

得分: 5

Fprintf的第一个参数是格式定义。"%2B"被解释为格式化指令,但你缺少了后续的参数。

也许你想使用Fprint

英文:

Fprintf takes a format definition as the first argument. The "%2B" is interpreted as a formatting directive and you are missing the following argument.

Maybe you wanted to use Fprint?

huangapple
  • 本文由 发表于 2014年7月30日 17:36:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/25033412.html
匿名

发表评论

匿名网友

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

确定