package core
import (
"github.com/zeromicro/go-zero/core/logx"
"fmt"
)
proto文件如下:
type Messages_Dialogs struct {
state protoimpl.MessageState `protogen:"open.v1"`
PredicateName string `protobuf:"bytes,1,opt,name=predicate_name,json=predicateName,proto3" json:"predicate_name,omitempty"`
Constructor TLConstructor `protobuf:"varint,2,opt,name=constructor,proto3,enum=mtproto.TLConstructor" json:"constructor,omitempty"`
Dialogs []*Dialog `protobuf:"bytes,3,rep,name=dialogs,proto3" json:"dialogs,omitempty"`
Messages []*Message `protobuf:"bytes,4,rep,name=messages,proto3" json:"messages,omitempty"`
Chats []*Chat `protobuf:"bytes,5,rep,name=chats,proto3" json:"chats,omitempty"`
Users []*User `protobuf:"bytes,6,rep,name=users,proto3" json:"users,omitempty"`
Count int32 `protobuf:"varint,7,opt,name=count,proto3" json:"count,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
//模拟同样的Messages_Dialogs数据
messageDialogs := Messages_Dialogs{}
fmt.Printf("会话最终数据:%s\n",messageDialogs)
logx.Logger.Debugf("messageDialogs-result:%s", messageDialog)
其中fmt.Printf输出结果:
&{Dialogs:[predicate_name:"dialog" peer:{predicate_name:"peerChannel" channel_id:1} top_message:4 read_inbox_max_id:4 read_outbox_max_id:3 notify_settings:{predicate_name:"peerNotifySettings"} pts:{value:5} predicate_name:"dialog" peer:{predicate_name:"peerUser" user_id:2} top_message:249 read_inbox_max_id:248 read_outbox_max_id:248 notify_settings:{predicate_name:"peerNotifySettings"} draft:{predicate_name:"draftMessageEmpty"} predicate_name:"dialog" peer:{predicate_name:"peerChannel" channel_id:5} top_message:2 read_inbox_max_id:2 notify_settings:{predicate_name:"peerNotifySettings"} pts:{value:3} predicate_name:"dialog" peer:{predicate_name:"peerChannel" channel_id:7} top_message:2 read_inbox_max_id:2 notify_settings:{predicate_name:"peerNotifySettings"} pts:{value:3} predicate_name:"dialog" peer:{predicate_name:"peerChannel" channel_id:9} top_message:1 read_inbox_max_id:1 notify_settings:{predicate_name:"peerNotifySettings"} pts:{value:2} predicate_name:"dialog" peer:{predicate_name:"peerChannel" channel_id:8} top_message:1 read_inbox_max_id:1 notify_settings:{predicate_name:"peerNotifySettings"} pts:{value:2} predicate_name:"dialog" peer:{predicate_name:"peerChannel" channel_id:6} top_message:1 read_inbox_max_id:1 notify_settings:{predicate_name:"peerNotifySettings"} pts:{value:2} predicate_name:"dialog" peer:{predicate_name:"peerChannel" channel_id:4} top_message:1 read_inbox_max_id:1 notify_settings:{predicate_name:"peerNotifySettings"} pts:{value:2} predicate_name:"dialog" peer:{predicate_name:"peerChannel" channel_id:3} top_message:1 read_inbox_max_id:1 notify_settings:{predicate_name:"peerNotifySettings"} pts:{value:2} predicate_name:"dialog" peer:{predicate_name:"peerChannel" channel_id:2} top_message:1 read_inbox_max_id:1 notify_settings:{predicate_name:"peerNotifySettings"} pts:{value:2} predicate_name:"dialog" peer:{predicate_name:"peerUser" user_id:3} top_message:196 read_outbox_max_id:248 notify_settings:{predicate_name:"peerNotifySettings"} draft:{predicate_name:"draftMessage" message:"A" date_INT32:1776225013}] Messages:[predicate_name:"message" id:196 peer_id:{predicate_name:"peerUser" user_id:3} out:true edit_hide:true from_id:{predicate_name:"peerUser" user_id:3} saved_peer_id:{predicate_name:"peerUser" user_id:3} date:1776222564 message:"怎么回事" predicate_name:"message" id:196 peer_id:{predicate_name:"peerUser" user_id:3} out:true edit_hide:true from_id:{predicate_name:"peerUser" user_id:3} saved_peer_id:{predicate_name:"peerUser" user_id:3} date:1776222564 message:"怎么回事" predicate_name:"messageService" id:1 peer_id:{predicate_name:"peerChannel" channel_id:3} out:true from_id:{predicate_name:"peerUser" user_id:3} date:1776332777 replies:{predicate_name:"messageReplies" replies_pts:2} action:{predicate_name:"messageActionChannelCreate" title_STRING:"das auto" title:"das auto"} predicate_name:"message" id:4 peer_id:{predicate_name:"peerChannel" channel_id:1} out:true edit_hide:true from_id:{predicate_name:"peerUser" user_id:3} date:1776680465 message:"来吧" replies:{predicate_name:"messageReplies" replies_pts:5} predicate_name:"messageService" id:2 peer_id:{predicate_name:"peerChannel" channel_id:7} out:true from_id:{predicate_name:"peerUser" user_id:3} date:1776411969 replies:{predicate_name:"messageReplies" replies_pts:3} action:{predicate_name:"messageActionChatAddUser" users:2} predicate_name:"messageService" id:1 peer_id:{predicate_name:"peerChannel" channel_id:9} out:true from_id:{predicate_name:"peerUser" user_id:3} date:1776346529 replies:{predicate_name:"messageReplies" replies_pts:2} action:{predicate_name:"messageActionChannelCreate" title_STRING:"das auto 6" title:"das auto 6"} predicate_name:"messageService" id:1 peer_id:{predicate_name:"peerChannel" channel_id:6} out:true from_id:{predicate_name:"peerUser" user_id:3} date:1776339437 replies:{predicate_name:"messageReplies" replies_pts:2} action:{predicate_name:"messageActionChannelCreate" title_STRING:"das auto3" title:"das auto3"} predicate_name:"messageService" id:1 peer_id:{predicate_name:"peerChannel" channel_id:2} out:true from_id:{predicate_name:"peerUser" user_id:3} date:1776319140 replies:{predicate_name:"messageReplies" replies_pts:2} action:{predicate_name:"messageActionChannelCreate" title_STRING:"明天" title:"明天"} predicate_name:"messageService" id:2 peer_id:{predicate_name:"peerChannel" channel_id:5} out:true from_id:{predicate_name:"peerUser" user_id:3} date:1776414606 replies:{predicate_name:"messageReplies" replies_pts:3} action:{predicate_name:"messageActionChatAddUser" users:2} predicate_name:"messageService" id:1 peer_id:{predicate_name:"peerChannel" channel_id:8} out:true from_id:{predicate_name:"peerUser" user_id:3} date:1776345155 replies:{predicate_name:"messageReplies" replies_pts:2} action:{predicate_name:"messageActionChannelCreate" title_STRING:"das auto 5" title:"das auto 5"} predicate_name:"messageService" id:1 peer_id:{predicate_name:"peerChannel" channel_id:4} out:true from_id:{predicate_name:"peerUser" user_id:3} date:1776337901 replies:{predicate_name:"messageReplies" replies_pts:2} action:{predicate_name:"messageActionChannelCreate" title_STRING:"das auto" title:"das auto"}] Chats:[predicate_name:"channel" id:1 creator:true title:"今天" photo:{predicate_name:"chatPhotoEmpty"} date:1776239001 version:4 admin_rights:{predicate_name:"chatAdminRights" change_info:true post_messages:true edit_messages:true delete_messages:true ban_users:true invite_users:true pin_messages:true add_admins:true manage_call:true other:true} default_banned_rights:{predicate_name:"chatBannedRights" group_member_chat:true until_date:2147483647} megagroup:true access_hash_FLAGINT64:{value:3100306701767489967} username:{value:"1"} predicate_name:"channel" id:5 creator:true title:"das auto2" photo:{predicate_name:"chatPhotoEmpty"} date:1776414606 version:4 admin_rights:{predicate_name:"chatAdminRights" change_info:true post_messages:true edit_messages:true delete_messages:true ban_users:true invite_users:true pin_messages:true add_admins:true manage_call:true other:true} default_banned_rights:{predicate_name:"chatBannedRights" group_member_chat:true until_date:2147483647} megagroup:true access_hash_FLAGINT64:{value:1250192832199693120} username:{value:"5"} predicate_name:"channel" id:7 creator:true title:"das auto 4" photo:{predicate_name:"chatPhotoEmpty"} date:1776411969 version:4 admin_rights:{predicate_name:"chatAdminRights" change_info:true post_messages:true edit_messages:true delete_messages:true ban_users:true invite_users:true pin_messages:true add_admins:true manage_call:true other:true} default_banned_rights:{predicate_name:"chatBannedRights" group_member_chat:true until_date:2147483647} megagroup:true access_hash_FLAGINT64:{value:5817603493844452254} username:{value:"7"} predicate_name:"channel" id:9 creator:true title:"das auto 6" photo:{predicate_name:"chatPhotoEmpty"} date:1776408927 version:4 admin_rights:{predicate_name:"chatAdminRights" change_info:true post_messages:true edit_messages:true delete_messages:true ban_users:true invite_users:true pin_messages:true add_admins:true manage_call:true other:true} default_banned_rights:{predicate_name:"chatBannedRights" group_member_chat:true until_date:2147483647} megagroup:true access_hash_FLAGINT64:{value:6565560585761123375} username:{value:"9"} predicate_name:"channel" id:8 creator:true title:"das auto 5" photo:{predicate_name:"chatPhotoEmpty"} date:1776345155 version:3 admin_rights:{predicate_name:"chatAdminRights" change_info:true post_messages:true edit_messages:true delete_messages:true ban_users:true invite_users:true pin_messages:true add_admins:true manage_call:true other:true} default_banned_rights:{predicate_name:"chatBannedRights" group_member_chat:true until_date:2147483647} megagroup:true access_hash_FLAGINT64:{value:3362468628012234069} username:{value:"8"} predicate_name:"channel" id:6 creator:true title:"das auto3" photo:{predicate_name:"chatPhotoEmpty"} date:1776409291 version:4 admin_rights:{predicate_name:"chatAdminRights" change_info:true post_messages:true edit_messages:true delete_messages:true ban_users:true invite_users:true pin_messages:true add_admins:true manage_call:true other:true} default_banned_rights:{predicate_name:"chatBannedRights" group_member_chat:true until_date:2147483647} megagroup:true access_hash_FLAGINT64:{value:7665386859116185218} username:{value:"6"} predicate_name:"channel" id:4 creator:true title:"das auto" photo:{predicate_name:"chatPhotoEmpty"} date:1776337901 version:3 admin_rights:{predicate_name:"chatAdminRights" change_info:true post_messages:true edit_messages:true delete_messages:true ban_users:true invite_users:true pin_messages:true add_admins:true manage_call:true other:true} default_banned_rights:{predicate_name:"chatBannedRights" group_member_chat:true until_date:2147483647} megagroup:true access_hash_FLAGINT64:{value:4596637112176182738} username:{value:"4"} predicate_name:"channel" id:3 creator:true title:"das auto" photo:{predicate_name:"chatPhotoEmpty"} date:1776332777 version:3 admin_rights:{predicate_name:"chatAdminRights" change_info:true post_messages:true edit_messages:true delete_messages:true ban_users:true invite_users:true pin_messages:true add_admins:true manage_call:true other:true} default_banned_rights:{predicate_name:"chatBannedRights" group_member_chat:true until_date:2147483647} megagroup:true access_hash_FLAGINT64:{value:3244627568738137573} username:{value:"3"} predicate_name:"channel" id:2 creator:true title:"明天" photo:{predicate_name:"chatPhotoEmpty"} date:1776319140 version:3 admin_rights:{predicate_name:"chatAdminRights" change_info:true post_messages:true edit_messages:true delete_messages:true ban_users:true invite_users:true pin_messages:true add_admins:true manage_call:true other:true} default_banned_rights:{predicate_name:"chatBannedRights" group_member_chat:true until_date:2147483647} megagroup:true access_hash_FLAGINT64:{value:902415847617661293} username:{value:"2"}] Users:[predicate_name:"user" id:3 self:true contact:true mutual_contact:true premium:true stories_unavailable:true access_hash:{value:5482441389841693861} first_name:{value:"weiting456"} username:{value:"weiting456"} phone:{value:"weiting456"} status:{predicate_name:"userStatusOnline" expires:1776692303} gender:1 predicate_name:"user" id:2 contact:true mutual_contact:true premium:true stories_unavailable:true access_hash:{value:6611045703465005276} first_name:{value:"阿门1"} last_name:{value:"weiting123456"} username:{value:"weiting123456"} phone:{value:"weiting123456"} status:{predicate_name:"userStatusOffline" was_online:1776264927} gender:1]}
logx.Logger.Debugf输出结果:
&{[predicate_name:\"dialog\" peer:{predicate_name:\"peerChannel\" channel_id:1} top_message:4 read_inbox_max_id:4 read_outbox_max_id:3 notify_settings:{predicate_name:\"peerNotifySettings\"} pts:{value:5} predicate_name:\"dialog\" peer:{predicate_name:\"peerUser\" user_id:2} top_message:249 read_inbox_max_id:248 read_outbox_max_id:248 notify_settings:{predicate_name:\"peerNotifySettings\"} draft:{predicate_name:\"draftMessageEmpty\"} predicate_name:\"dialog\" peer:{predicate_name:\"peerChannel\" channel_id:5} top_message:2 read_inbox_max_id:2 notify_settings:{predicate_name:\"peerNotifySettings\"} pts:{value:3} predicate_name:\"dialog\" peer:{predicate_name:\"peerChannel\" channel_id:7} top_message:2 read_inbox_max_id:2 notify_settings:{predicate_name:\"peerNotifySettings\"} pts:{value:3} predicate_name:\"dialog\" peer:{predicate_name:\"peerChannel\" channel_id:9} top_message:1 read_inbox_max_id:1 notify_settings:{predicate_name:\"peerNotifySettings\"} pts:{value:2} predicate_name:\"dialo","level":"debug","span":"ca259e08b5e1a1bc","trace":"ab148bae845ab7e02632dec47197577b","truncated":true}
我的问题是,都是一样的结构体数据,输出的差异怎么这么大
golang printf打印数据和Logger.Debugf打印数据明显差异
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
檀越@新空间 2026-04-20 22:00关注晚上好🌙🌙🌙
本答案参考通义千问你遇到的问题是
fmt.Printf和logx.Logger.Debugf打印的Messages_Dialogs数据结果不一致。这个问题的核心在于 Go 的默认格式化方式和logx库对结构体的处理方式不同。
问题分析
-
fmt.Printf("会话最终数据:%s\n", messageDialogs)- Go 的
fmt包会对结构体使用默认的格式化方式,也就是打印其字段名和值。 - 对于包含嵌套结构体或复杂字段(如
*Dialog、*Message等)的结构体,fmt.Printf只能打印出结构体的内存地址或部分信息,无法深入显示嵌套内容。 - 因此,
fmt.Printf输出的是结构体的 内存地址 或 基本字段值,而不是完整的内容。
- Go 的
-
logx.Logger.Debugf("messageDialogs-result:%s", messageDialog)logx是一个封装了日志功能的库,它内部可能通过反射或自定义方式处理结构体的格式化输出。- 如果
logx使用了fmt.Sprintf("%v", messageDialog),那么它会尝试打印整个结构体的内容,包括嵌套结构体的字段。 - 但如果你没有正确地实现
Stringer接口或未进行深度格式化,logx也可能只打印部分信息。
解决方案
✅ 解决方案 1:使用
fmt.Sprintf("%+v", messageDialogs)package core import ( "fmt" "github.com/zeromicro/go-zero/core/logx" ) func main() { messageDialogs := Messages_Dialogs{} logx.Logger.Debugf("messageDialogs-result:%+v", messageDialogs) fmt.Printf("会话最终数据:%+v\n", messageDialogs) }%+v会以更详细的方式打印结构体,包括字段名和值。- 这样可以确保两个方法都打印出完整的结构体内容。
✅ 解决方案 2:为结构体实现
Stringer接口你可以为
Messages_Dialogs实现String()方法,这样无论是fmt.Printf还是logx都会调用这个方法来打印结构体。type Messages_Dialogs struct { // ... 其他字段 ... } func (m *Messages_Dialogs) String() string { return fmt.Sprintf("Messages_Dialogs{PredicateName: %s, Constructor: %d, Dialogs: %v, Messages: %v, Chats: %v, Users: %v, Count: %d}", m.PredicateName, m.Constructor, m.Dialogs, m.Messages, m.Chats, m.Users, m.Count) }- 该方法将返回一个字符串表示的结构体内容。
- 无论
fmt.Printf还是logx.Debugf都会使用这个String()方法来打印。
✅ 解决方案 3:使用
json.Marshal格式化输出如果你需要更清晰的结构化输出,可以使用 JSON 格式:
import ( "encoding/json" "fmt" "github.com/zeromicro/go-zero/core/logx" ) func main() { messageDialogs := Messages_Dialogs{} // 使用 JSON 格式打印 jsonBytes, _ := json.MarshalIndent(messageDialogs, "", " ") logx.Logger.Debugf("messageDialogs-result:\n%s", jsonBytes) fmt.Printf("会话最终数据:\n%s\n", jsonBytes) }- 优点:JSON 格式清晰易读,适合调试。
- 缺点:如果结构体中包含指针或非导出字段,可能会导致错误。
✅ 重点总结
| 问题 | 原因 | 解决方案 | |------|------|----------| |
fmt.Printf和logx.Debugf打印结果不一致 |fmt.Printf默认只打印结构体地址或部分字段,而logx可能尝试打印完整结构体 | 使用%+v、实现Stringer接口、或使用json.Marshal| | 结构体嵌套字段未被完整打印 | Go 默认不支持递归打印嵌套结构体 | 实现Stringer或使用json.Marshal|
🧩 修改后的代码示例
package core import ( "encoding/json" "fmt" "github.com/zeromicro/go-zero/core/logx" ) type Messages_Dialogs struct { state protoimpl.MessageState `protogen:"open.v1"` PredicateName string `protobuf:"bytes,1,opt,name=predicate_name,json=predicateName,proto3" json:"predicate_name,omitempty"` Constructor TLConstructor `protobuf:"varint,2,opt,name=constructor,proto3,enum=mtproto.TLConstructor" json:"constructor,omitempty"` Dialogs []*Dialog `protobuf:"bytes,3,rep,name=dialogs,proto3" json:"dialogs,omitempty"` Messages []*Message `protobuf:"bytes,4,rep,name=messages,proto3" json:"messages,omitempty"` Chats []*Chat `protobuf:"bytes,5,rep,name=chats,proto3" json:"chats,omitempty"` Users []*User `protobuf:"bytes,6,rep,name=users,proto3" json:"users,omitempty"` Count int32 `protobuf:"varint,7,opt,name=count,proto3" json:"count,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } // 实现 Stringer 接口 func (m *Messages_Dialogs) String() string { return fmt.Sprintf("Messages_Dialogs{PredicateName: %s, Constructor: %d, Dialogs: %v, Messages: %v, Chats: %v, Users: %v, Count: %d}", m.PredicateName, m.Constructor, m.Dialogs, m.Messages, m.Chats, m.Users, m.Count) } func main() { messageDialogs := &Messages_Dialogs{} // 使用 Stringer 接口 logx.Logger.Debugf("messageDialogs-result: %s", messageDialogs) fmt.Printf("会话最终数据: %s\n", messageDialogs) // 使用 JSON 格式化 jsonBytes, _ := json.MarshalIndent(messageDialogs, "", " ") logx.Logger.Debugf("messageDialogs-result:\n%s", jsonBytes) fmt.Printf("会话最终数据:\n%s\n", jsonBytes) }
✅ 最终建议
- 推荐使用
Stringer接口,因为它是 Go 中最标准的结构体格式化方式。 - 对于复杂结构体,使用
json.Marshal是最清晰的调试方式。 - 避免直接打印结构体变量(如
messageDialogs),除非你已实现Stringer或使用fmt.Sprintf("%+v", messageDialogs)。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报-