| 545 | } |
| 546 | |
| 547 | func Example() { |
| 548 | const ( |
| 549 | defaultName = "some name" |
| 550 | ) |
| 551 | |
| 552 | // Stuff contains optional fields. |
| 553 | type Stuff struct { |
| 554 | Name *string |
| 555 | Comment *string |
| 556 | Value *int64 |
| 557 | Time *time.Time |
| 558 | } |
| 559 | |
| 560 | b, _ := json.Marshal(&Stuff{ |
| 561 | Name: ToString(defaultName), // can't say &defaultName |
| 562 | Comment: ToString("not yet"), // can't say &"not yet" |
| 563 | Value: ToInt64(42), // can't say &42 or &int64(42) |
| 564 | Time: ToTime(time.Date(2014, 6, 25, 12, 24, 40, 0, time.UTC)), // can't say &time.Date(…) |
| 565 | }) |
| 566 | |
| 567 | fmt.Printf("%s", b) |
| 568 | |
| 569 | // Output: {"Name":"some name","Comment":"not yet","Value":42,"Time":"2014-06-25T12:24:40Z"} |
| 570 | } |