()
| 293 | } |
| 294 | |
| 295 | func ExampleContext_Objects() { |
| 296 | // In go, arrays are type invariant so even if you have a variable u of type []User array and User implements |
| 297 | // the LogObjectMarshaler interface, you cannot pass that to func that takes an []LogObjectMarshaler array in the |
| 298 | // Objects call. In 1.24+ it allows passing the variadic covariant slice (e.g. u...) but the unit test needs to |
| 299 | // work in earlier versions so we'll declare the array as []LogObjectMarshaler here. |
| 300 | u := []LogObjectMarshaler{User{"John", 35, time.Time{}}, User{"Bob", 55, time.Time{}}} |
| 301 | |
| 302 | dst := bytes.Buffer{} |
| 303 | log := New(&dst).With(). |
| 304 | Str("foo", "bar"). |
| 305 | Objects("users", u). |
| 306 | Logger() |
| 307 | |
| 308 | log.Log().Msg("hello world") |
| 309 | |
| 310 | fmt.Println(decodeIfBinaryToString(dst.Bytes())) |
| 311 | // Output: {"foo":"bar","users":[{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},{"name":"Bob","age":55,"created":"0001-01-01T00:00:00Z"}],"message":"hello world"} |
| 312 | } |
| 313 | |
| 314 | func ExampleEvent_EmbedObject() { |
| 315 | price := Price{val: 6449, prec: 2, unit: "$"} |
nothing calls this directly
no test coverage detected
searching dependent graphs…