#Writing text to JSON file, but empty

23 messages · Page 1 of 1 (latest)

pure orbit
#

You never write to newData

#

So you're just writing out an empty map every time

#

You're encoding an empty map

#

It looks like you're missing a step where you unmarshal the body into the map.

#

Not sure why you changed to writefile, but the error explains what is wrong.

#

The encoder would work fine for writing to a file

#

encoding will write to the writer you give it, so there's no need for the writefile

#

Sure, now you're dealing with bytes and need to write those to the file

#

The encoder was taking care of that, but marshal just hands you back the bytes

#

That's not what I linked

pure orbit
#

You are trying to write to a readonly file

#

os.Open opens a file in readonly

#

You likely meant file_create.Write

#

I know

#

Your code is trying to write to the test.txt file

#

Because you used file.Write instead of file_create.Write

#

As an aside, os.Create will truncate the file, so each loop is overwriting your things.json

#

No, os.WriteFile just returns an error.
You can do what you were trying to do, you just need to write to the right file

#

_, err = file.Write(jsonData)

#

That code is trying to write to a readonly file test.txt

pure orbit
#

Sure, the return signature of File.Write is (n int, err error)