Avatar ·

如何在Go中将结构体(struct)内容输出到控制台?

大家好!我刚开始学习Go,在调试时遇到了一个问题。我创建了一个`User`结构体,包含`Name`、`Age`和`Email`字段。当我尝试用`fmt.Println(user)`将其输出到控制台时,看到的是类似`{John 30 john@example.com}`这样的内容,但可读性不太好。特别是当结构体很大或包含嵌套结构体时。 这是我的代码: ```go package main import "fmt" type Address struct { City string Street string ZipCode int } type User struct { Name string Age int Email string Address Address } func main() { user := User{ Name: "John Doe", Age: 30, Email: "john@example.com", Address: Address{ City: "New York", Street: "5th Avenue", ZipCode: 10001, }, } fmt.Println(user) // 输出:{John Doe 30 john@example.com {New York 5th Avenue 10001}} } ``` 我希望看到字段名和