String returns current array as a string, which implements like json.Marshal does.
()
| 384 | |
| 385 | // String returns current array as a string, which implements like json.Marshal does. |
| 386 | func (a *SortedStrArray) String() string { |
| 387 | if a == nil { |
| 388 | return "" |
| 389 | } |
| 390 | a.lazyInit() |
| 391 | a.mu.RLock() |
| 392 | defer a.mu.RUnlock() |
| 393 | buffer := bytes.NewBuffer(nil) |
| 394 | buffer.WriteByte('[') |
| 395 | for k, v := range a.array { |
| 396 | buffer.WriteString(`"` + gstr.QuoteMeta(v, `"\`) + `"`) |
| 397 | if k != len(a.array)-1 { |
| 398 | buffer.WriteByte(',') |
| 399 | } |
| 400 | } |
| 401 | buffer.WriteByte(']') |
| 402 | return buffer.String() |
| 403 | } |
| 404 | |
| 405 | // MarshalJSON implements the interface MarshalJSON for json.Marshal. |
| 406 | // Note that do not use pointer as its receiver here. |