Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package json_test | 5 package json_test |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "encoding/json" | 9 "encoding/json" |
10 "fmt" | 10 "fmt" |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 // YCbCr &{255 0 -10} | 128 // YCbCr &{255 0 -10} |
129 // RGB &{98 218 255} | 129 // RGB &{98 218 255} |
130 } | 130 } |
131 | 131 |
132 func ExampleIndent() { | 132 func ExampleIndent() { |
133 type Road struct { | 133 type Road struct { |
134 Name string | 134 Name string |
135 Number int | 135 Number int |
136 } | 136 } |
137 roads := []Road{ | 137 roads := []Road{ |
138 » » Road{"Diamond Fork", 29}, | 138 » » {"Diamond Fork", 29}, |
139 » » Road{"Sheep Creek", 51}, | 139 » » {"Sheep Creek", 51}, |
140 } | 140 } |
141 | 141 |
142 b, err := json.Marshal(roads) | 142 b, err := json.Marshal(roads) |
143 if err != nil { | 143 if err != nil { |
144 log.Fatal(err) | 144 log.Fatal(err) |
145 } | 145 } |
146 | 146 |
147 var out bytes.Buffer | 147 var out bytes.Buffer |
148 » out.Write([]byte("={\n=\t\"destinations\": ")) | 148 » json.Indent(&out, b, "=", "\t") |
149 » // the previous write did not end with newline, and the following does | |
150 » // not start with the "=" | |
151 » json.Indent(&out, b, "=\t", "\t") | |
152 » out.Write([]byte("\n=}\n")) | |
153 | |
154 out.WriteTo(os.Stdout) | 149 out.WriteTo(os.Stdout) |
155 // Output: | 150 // Output: |
adg
2014/05/05 01:53:31
Wouldn't it be clearer to write an example that on
smcquay
2014/05/07 03:41:34
The behavior of Indent wasn't clear to me until I
adg
2014/05/08 04:00:50
I'd like to take a look at a stripped back example
smcquay
2014/05/08 06:24:26
Done.
| |
156 » // ={ | 151 » // [ |
157 » // =» "destinations": [ | 152 » // = { |
158 » // =» » { | 153 » // = "Name": "Diamond Fork", |
159 » // =» » » "Name": "Diamond Fork", | 154 » // = "Number": 29 |
160 » // =» » » "Number": 29 | 155 » // = }, |
161 » // =» » }, | 156 » // = { |
162 » // =» » { | 157 » // = "Name": "Sheep Creek", |
163 » // =» » » "Name": "Sheep Creek", | 158 » // = "Number": 51 |
164 » // =» » » "Number": 51 | 159 » // = } |
165 » // =» » } | 160 » // =] |
166 » // =» ] | |
167 » // =} | |
168 } | 161 } |
LEFT | RIGHT |