Standard library / log/slog

slog.TextHandler.WithGroup()

Return a handler qualifying subsequent attributes with a group name. Expected stdout: 'level=INFO msg=ready demo.n=2\n'.

Formal Syntax & Structure

go syntaxgo
func (h *TextHandler) WithGroup(name string) Handler

Parameters & Attributes

NameTypeDescription
namestringGroup name.

Return Value

Handler

Return a handler qualifying subsequent attributes with a group name.

Working Example

Example Snippetgo
package main
import (  "bytes"  "context"  "fmt"  "log/slog"  "time")
func check(e error) {  if e != nil {    panic(e)  }}
func main() {  var b bytes.Buffer  h := slog.NewTextHandler(&b, nil)  child := h.WithGroup("demo")  r := slog.NewRecord(time.Time{}, slog.LevelInfo, "ready", 0)  r.Add("n", 2)  check(child.Handle(context.Background(), r))  fmt.Print(b.String())}
Go Reference