0

I have the next structure:

type filesDB struct {
    Name    string
    Content []byte
}

type fileDB struct {
  Files map[string]filesDB
}

Well, when I try to save data in JSON format with the maps, I try this:

First, I make a map var:

var filesDatabase = map[string]fileDB{}

Then, i try to insert data:

    var f filesDB
    fidb := map[string]filesDB{}
    f.Name= filename
    f.Content= encrypt(b, sUser[user].Cipher)
    fidb[filename] = f
    filesDatabase[user].Files = fidb
    jsonDB, err := json.Marshal(filesDatabase)

So, when I try to run the script, I get the next error:

cannot assign to struct field filesDatabase[user].Files in map

How can I resolve this error? Thanks!

nyi
  • 3,123
  • 4
  • 22
  • 45
Valdemore
  • 13
  • 1
  • 5
  • 1
    duplicate https://stackoverflow.com/questions/32751537/why-do-i-get-a-cannot-assign-error-when-setting-value-to-a-struct-as-a-value-i and https://stackoverflow.com/questions/40578646/golang-i-have-a-map-of-structs-why-cant-i-directly-modify-a-field-in-a-struct – mkopriva May 04 '18 at 20:54
  • 1
    [what you should do](https://play.golang.org/p/kYoiZdwpXyK) – mkopriva May 04 '18 at 20:59
  • Very thanks mkopriva and very fast! – Valdemore May 04 '18 at 21:05

0 Answers0