String returns current array as a string, which implements like json.Marshal does.
()
| 441 | |
| 442 | // String returns current array as a string, which implements like json.Marshal does. |
| 443 | func (a *StrArray) String() string { |
| 444 | if a == nil { |
| 445 | return "" |
| 446 | } |
| 447 | |
| 448 | a.lazyInit() |
| 449 | |
| 450 | a.mu.RLock() |
| 451 | defer a.mu.RUnlock() |
| 452 | buffer := bytes.NewBuffer(nil) |
| 453 | buffer.WriteByte('[') |
| 454 | for k, v := range a.array { |
| 455 | buffer.WriteString(`"` + gstr.QuoteMeta(v, `"\`) + `"`) |
| 456 | if k != len(a.array)-1 { |
| 457 | buffer.WriteByte(',') |
| 458 | } |
| 459 | } |
| 460 | buffer.WriteByte(']') |
| 461 | return buffer.String() |
| 462 | } |
| 463 | |
| 464 | // MarshalJSON implements the interface MarshalJSON for json.Marshal. |
| 465 | // Note that do not use pointer as its receiver here. |