I write A
object to a file f
.
a := A{42}
bytes, _ := json.MarshalIndent(a, "", "\t")
f.Write(bytes)
Where A
looks like:
type A struct {
A int `json:"a"`
}
Then I change field of this object and write it to the file:
a.A = 666
f.Write(bytes)
As a result, I see only
{
"a": 42
}{
"a": 42
}
While I expected:
{
"a": 42
}{
"a": 666
}
I know that I can overcome it by using json.MarshalIndent
again. But I need to do a lot of (~10^6) writings to the file, so using json.MarshalIndent
again and again seems to be a heavy task.
How can I directly change a bytes
variable?
Code is located at https://play.golang.org/p/8CMpwehMidR