使用 os.stdout.write 和新行输出 json?

发布于 2025-01-14 08:35:41 字数 675 浏览 0 评论 0原文

这里是 golang 新手。我想简单地将一些 json 输出到标准输出以进行调试。我想我对这一切的看法都是错误的。这就是我所拥有的:

type SomeObject struct {
  Thing1 string
  Thing2 string
  Thing3 otherStruct
}

...

someObject = &SomeObject{
  Thing1: "hello",
  Thing2: "world",
  ...
}
someObjectLogEntry, err := json.Marshal(someObject)
if err != nil {
    fmt.Println("error:", err)
}
os.Stdout.Write(someObjectLogEntry)

当我运行此命令时,它将 json 输出为一行,但是我的服务也有一个心跳,因此两者重合并将两件事输出在同一行中,例如:

{/// All that json content }[GIN] 2022/03/16 - 02:07:16 | 200 | 1.16µs | 127.0.0.1 | GET "/heartbeat"

正确的方法是什么我在做什么(简单地构造一个 json 对象并输出它)?如果我执行fmt.println,它将打印出字节码。谢谢!

new to golang here. I wanted to simply output some json to stdout for debug purposes. I think I'm going about this all wrong. Here is what I have:

type SomeObject struct {
  Thing1 string
  Thing2 string
  Thing3 otherStruct
}

...

someObject = &SomeObject{
  Thing1: "hello",
  Thing2: "world",
  ...
}
someObjectLogEntry, err := json.Marshal(someObject)
if err != nil {
    fmt.Println("error:", err)
}
os.Stdout.Write(someObjectLogEntry)

When I run this, it outputs the json as one line, however my service also has a heartbeat going and so the two coincide and output both things in the same line, something like:

{/// All that json content }[GIN] 2022/03/16 - 02:07:16 | 200 | 1.16µs | 127.0.0.1 | GET "/heartbeat"

What's the correct way to do what I'm doing (simply constructing a json object and outputting it)? If i do fmt.println it will then print out the byte code. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

变身佩奇 2025-01-21 08:35:42

最干净的方法是使用 json.Encoder 到 os.Stdout。

json.Encoder 已经在每条写入的消息中附加了换行符,并且比使用带有 json 字节的 fmt 更有效。

您可以重复使用 json 编码器,而不是调用 json.Marshal,然后为每条消息进行某种类型的单独写入。

import (
   "os"
   "encoding/json"
)

type SomeObject struct {
  Thing1 string
  Thing2 string
  Thing3 otherStruct
}

...

encoder := json.NewEncoder(os.Stdout)

someObject = &SomeObject{
  Thing1: "hello",
  Thing2: "world",
  ...
}

if err := encoder.Encode(&someObject); err != nil {
    // handle error
}


The cleanest approach is to use json.Encoder to os.Stdout.

json.Encoder already appends a newline to each message written and is more efficient than using fmt with json bytes.

You can re-use the json encoder instead of calling json.Marshal then some type of separate write for each message.

import (
   "os"
   "encoding/json"
)

type SomeObject struct {
  Thing1 string
  Thing2 string
  Thing3 otherStruct
}

...

encoder := json.NewEncoder(os.Stdout)

someObject = &SomeObject{
  Thing1: "hello",
  Thing2: "world",
  ...
}

if err := encoder.Encode(&someObject); err != nil {
    // handle error
}


左耳近心 2025-01-21 08:35:41

您需要输出换行符或换行符 (\n)在 JSON 之后。最简单的方法可能是使用 fmt.Printf 例如 (游乐场

type SomeObject struct {
    Thing1 string
    Thing2 string
}
someObject := &SomeObject{
    Thing1: "hello",
    Thing2: "world",
}
someObjectLogEntry, err := json.Marshal(someObject)
if err != nil {
    fmt.Println("error:", err)
}
fmt.Printf("%s\n", someObjectLogEntry)
os.Stdout.Write([]byte("something else\n"))

You need to output a line feed or newline (\n) after the JSON. The simplest approach is probably using fmt.Printf e.g. (playground)

type SomeObject struct {
    Thing1 string
    Thing2 string
}
someObject := &SomeObject{
    Thing1: "hello",
    Thing2: "world",
}
someObjectLogEntry, err := json.Marshal(someObject)
if err != nil {
    fmt.Println("error:", err)
}
fmt.Printf("%s\n", someObjectLogEntry)
os.Stdout.Write([]byte("something else\n"))
恬淡成诗 2025-01-21 08:35:41

使用 fmt 包中的 Printf 函数实现此目的的典型方法是在格式字符串中包含换行符 \n。您不一定需要将数据写入标准输出,因为 Printf 会为您完成此操作。

另一种选择是使用 fmt 包中的 Println 函数,该函数使用默认格式将其操作数写入标准输出。使用 Println 时,有必要将 JSON bytes 转换为 string。请注意,使用 Println 时,操作数之间始终添加空格并附加换行符。

使用 fmt.Println 时看到单个字节值的原因是因为字节切片被打印为字符串或切片的未解释字节 - 字节切片可以包含任何内容,而不仅仅是可打印字符。另一方面,os.Stdout.Write 将字节切片写入标准输出,并且您的终端可以正确呈现它们,因为它们是可打印字符。

The typical way to achieve this while using the Printf function from the fmt package is by including a newline character \n in the format string. You don’t necessarily need to write the data to standard output, because Printf does that for you.

Another option is to use the Println function from the fmt package which formats using the default formats for its operands writes to standard output. It’s necessary to convert your JSON bytes to string while using Println. Note that, spaces are always added between the operands and a newline is appended while using Println.

The reason that you see individual byte values while using fmt.Println is because a byte slice is printed out as uninterpreted bytes of the string or slice — the byte slice can contain anything at all, not just printable characters. On the other hand, os.Stdout.Write writes the byte slice to standard out and your terminal renders them properly because they are printable characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文