The Attr type

July 2, 2026

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) Attr

Any 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


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


slog.Handler

I’m probably doing this in backwards order, but that’s the order the GoDoc presents it… Now that we’ve looked at each of the methods of the Handler interface, we come to its general description: type Handler A Handler handles log records produced by a Logger. A typical handler may print log records to standard error, or write them to a file or database, or perhaps augment them with additional attributes and pass them on to another handler.


slog.Handler.WithGroup

I hope everyone had a good Labor Day weekend… even if you’re not in the US and had to/got to labor on Monday. Speaking of labor, I’m looking for new clients! If you could use some world class Go expertise on your project, please reach out! type Handler type Handler interface { … // WithGroup returns a new Handler with the given group appended to // the receiver's existing groups. // The keys of all subsequent attributes, whether added by With or in a // Record, should be qualified by the sequence of group names.


slog.Handler.WithAttrs

type Handler type Handler interface { … // WithAttrs returns a new Handler whose attributes consist of // both the receiver's attributes and the arguments. // The Handler owns the slice: it may retain, modify or discard it. WithAttrs(attrs []Attr) Handler This is likely the most interesting and subtle method of the slog.Handler interface. The important thing to note is that the handler is not mutated in place. Rather, a new handler is meant to be returned.

Get daily content like this in your inbox!

Subscribe