英文:
Go: How to pretty print nested objects with pointers values?
问题
如何对下面的对象进行漂亮的打印?
package main
// OBJECT: {
// TABLE: {
// files: [],
// data: {
// CODE: {
// name: "NAME",
// count: 123,
// }
// }
//
// }
import (
"encoding/json"
"fmt"
"log"
)
type Container map[string]*Table
type Table struct {
files []string
data map[string]*Data
}
type Data struct {
name string
count int
}
func main() {
object := Container{
"table1": {
files: []string{"file-1.1"},
data: map[string]*Data{
"XYZ": {
name: "foo",
count: 123,
},
},
},
"table2": {
files: []string{
"file-2.1",
"file-2.2",
"file-2.3",
},
},
"table3": {files: []string{"file-3.1"}},
}
fmt.Printf("%#v\n", &object)
objectJSON, err := json.MarshalIndent(object, "", " ")
if err != nil {
log.Fatalf(err.Error())
}
fmt.Printf("%s\n", objectJSON)
}
使用这段代码,我只能得到对象的第一层深度:
&main.Container{"table1":(*main.Table)(0xc00005c020), "table2":(*main.Table)(0xc00005c040), "table3":(*main.Table)(0xc00005c060)}
{
"table1": {},
"table2": {},
"table3": {}
}
程序已退出。
英文:
How can I pretty print the object below?
package main
// OBJECT: {
// TABLE: {
// files: [],
// data: {
// CODE: {
// name: "NAME",
// count: 123,
// }
// }
//
// }
import (
"encoding/json"
"fmt"
"log"
)
type Container map[string]*Table
type Table struct {
files []string
data map[string]*Data
}
type Data struct {
name string
count int
}
func main() {
object := Container{
"table1": {
files: []string{"file-1.1"},
data: map[string]*Data{
"XYZ": {
name: "foo",
count: 123,
},
},
},
"table2": {
files: []string{
"file-2.1",
"file-2.2",
"file-2.3",
},
},
"table3": {files: []string{"file-3.1"}},
}
fmt.Printf("%#v\n", &object)
objectJSON, err := json.MarshalIndent(object, "", " ")
if err != nil {
log.Fatalf(err.Error())
}
fmt.Printf("%s\n", objectJSON)
}
https://go.dev/play/p/FRWZsfwgyNU
With this code, I'm only getting the first depth of my object:
&main.Container{"table1":(*main.Table)(0xc00005c020), "table2":(*main.Table)(0xc00005c040), "table3":(*main.Table)(0xc00005c060)}
{
"table1": {},
"table2": {},
"table3": {}
}
Program exited.
答案1
得分: 2
这不是关于“深度”的问题。你不能使用fmt.Printf
或json.Marshal
来通用地漂亮打印私有的、未导出的字段。如果你希望在编组时出现该变量,请将其导出(即将Table.files
更改为Table.Files
)。Go语言将导出的字段编组到任意深度:
{
"foo1": {
"Files": [
"file-1.1"
],
"Data": {
"XYZ": {
"Name": "foo",
"Count": 123
}
}
},
"foo2": {
"Files": [
"file-2.1",
"file-2.2",
"file-2.3"
],
"Data": null
},
"foo3": {
"Files": [
"file-3.1"
],
"Data": null
}
}
如果你想使用fmt.Printf("%v", ...)
来漂亮打印对象,那么你需要在每个类上实现Stringer
接口。在那时,关于如何打印对象的决定完全取决于你,你可以以任何格式包括公共或私有成员。
英文:
This isn't about "depth". You can't generically pretty-print private, un-exported fields using fmt.Printf
or json.Marshal
. If you want the variable to appear when marshling, export them (ie, change Table.files
to Table.Files
. Go will marshal exported fields to arbitrary depth:
{
"foo1": {
"Files": [
"file-1.1"
],
"Data": {
"XYZ": {
"Name": "foo",
"Count": 123
}
}
},
"foo2": {
"Files": [
"file-2.1",
"file-2.2",
"file-2.3"
],
"Data": null
},
"foo3": {
"Files": [
"file-3.1"
],
"Data": null
}
}
If you want to pretty print the object using fmt.Printf("%v", ...)
then you need to implement the Stringer
interface on each of your class. At that point, the decision about how to print the object is entirely up to you and you can include public or private members in whatever format you'd like.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论