Standard library / log/slog

slog.Duration()

Construct a keyed Duration attribute for structured logging. Expected stdout: 'key\n2s\n'.

Formal Syntax & Structure

go syntaxgo
func Duration(key string, v time.Duration) Attr

Parameters & Attributes

NameTypeDescription
keystringAttribute key.
vtime.DurationValues to log or convert.

Return Value

Attr

Construct a keyed Duration attribute for structured logging.

Working Example

Example Snippetgo
package main
import (  "fmt"  "log/slog"  "time")
func main() { a := slog.Duration("key", 2*time.Second); fmt.Println(a.Key); fmt.Println(a.Value) }
Go Reference