-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
log/slog: LogValuer implementation panics on nil value #70069
Comments
this looks working as intended for slog, the issue is in your code |
Thanks for your reply! Just to confirm, do I need to handle nil values explicitly even if the method receiver is defined on a value rather than a pointer? Even without accessing struct fields, I'm encountering a panic: https://go.dev/play/p/vott61McxDf?v=gotip |
given a pointer, Go needs to dereference it to pass to the method. That's where your nil pointer dereference is coming from. It also panics if you call Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only. For questions please refer to https://github.com/golang/go/wiki/Questions |
I was just thinking it would be helpful if this behaved similarly to // If it's a nil pointer, output "<nil>". This approach could handle cases
// where a Stringer might not check for nil or where a nil pointer is used
// with a value receiver. In both scenarios, "<nil>" seems like a reasonable default.
if v := reflect.ValueOf(arg); v.Kind() == reflect.Pointer && v.IsNil() {
p.buf.writeString(nilAngleString)
return
} |
Go version
go1.23 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
Go Playground link
What did you see happen?
2009/11/10 23:00:00 INFO Message text foo="LogValue panicked\ncalled from runtime.panicwrap (/usr/local/go-faketime/src/runtime/error.go:355)\ncalled from main.(*foo).LogValue (:1)\ncalled from log/slog.Value.Resolve (/usr/local/go-faketime/src/log/slog/value.go:512)\ncalled from log/slog.(*handleState).appendAttr (/usr/local/go-faketime/src/log/slog/handler.go:468)\ncalled from log/slog.(*handleState).appendNonBuiltIns.func1 (/usr/local/go-faketime/src/log/slog/handler.go:342)\n(rest of stack elided)\n"
What did you expect to see?
2009/11/10 23:00:00 INFO Message text foo=
The text was updated successfully, but these errors were encountered: