@@ -34,6 +34,14 @@ func (s stringVal) Equal(o any) bool {
3434 return ok && s .s == os .s
3535}
3636
37+ type stringerVal struct {
38+ s string
39+ }
40+
41+ func (s stringerVal ) String () string {
42+ return s .s
43+ }
44+
3745func ExampleAttributes () {
3846 type keyOne struct {}
3947 type keyTwo struct {}
@@ -57,6 +65,33 @@ func ExampleAttributes_WithValue() {
5765 // Key two: {two}
5866}
5967
68+ func ExampleAttributes_String () {
69+ type key struct {}
70+ var typedNil * stringerVal
71+ a1 := attributes .New (key {}, typedNil ) // typed nil implements [fmt.Stringer]
72+ a2 := attributes .New (key {}, (* stringerVal )(nil )) // typed nil implements [fmt.Stringer]
73+ a3 := attributes .New (key {}, (* stringVal )(nil )) // typed nil not implements [fmt.Stringer]
74+ a4 := attributes .New (key {}, nil ) // untyped nil
75+ a5 := attributes .New (key {}, 1 )
76+ a6 := attributes .New (key {}, stringerVal {s : "two" })
77+ a7 := attributes .New (key {}, stringVal {s : "two" })
78+ fmt .Println ("a1:" , a1 .String ())
79+ fmt .Println ("a2:" , a2 .String ())
80+ fmt .Println ("a3:" , a3 .String ())
81+ fmt .Println ("a4:" , a4 .String ())
82+ fmt .Println ("a5:" , a5 .String ())
83+ fmt .Println ("a6:" , a6 .String ())
84+ fmt .Println ("a7:" , a7 .String ())
85+ // Output:
86+ // a1: {"<%!p(attributes_test.key={})>": "<nil>" }
87+ // a2: {"<%!p(attributes_test.key={})>": "<nil>" }
88+ // a3: {"<%!p(attributes_test.key={})>": "<0x0>" }
89+ // a4: {"<%!p(attributes_test.key={})>": "<nil>" }
90+ // a5: {"<%!p(attributes_test.key={})>": "<%!p(int=1)>" }
91+ // a6: {"<%!p(attributes_test.key={})>": "two" }
92+ // a7: {"<%!p(attributes_test.key={})>": "<%!p(attributes_test.stringVal={two})>" }
93+ }
94+
6095// Test that two attributes with the same content are Equal.
6196func TestEqual (t * testing.T ) {
6297 type keyOne struct {}
0 commit comments