Other than logger methods, the slog.Attr type is probably the thing you’ll use the most in your use of the slog package:
Types
type Attr
type Attr struct { Key string Value Value }An Attr is a key-value pair.
This type has several type-specific constructors, too, but we’re going to do an abbrevaited look at them, because they’re highly repetitive.
func Any
func Any(key string, value any) AttrAny returns an Attr for the supplied value. See AnyValue for how values are treated.
Any provides the general-purpose way to create an Attr for any type.
var foo struct {
Some int
Random string
Fields float64
}
slog.Any("foo", foo{})
slog.Any("bool", true) // Functionally equivalent to using slog.Bool
Notably, slog.Any can be used for types that also have their own constructors, listed below. The only difference in using the type-specific constructors is that they don’t have to do any runtime type assertion.
func Bool
func Duration
func Float64