【问题标题】:Saving Read/Write/Save to json from Struct of Map从 Map 的结构中保存读/写/保存到 json
【发布时间】:2019-05-30 07:16:37
【问题描述】:

我一直在尝试创建一个“工作”文件,我将应用程序的某些基本状态保存到该文件中,而不是将它们保存在 Ram 中,因为它们需要每天保存,我决定每天创建文件,这部分工作正常,但为了更清楚,我已将其从代码中删除。

现在我可以用信息结构的假值初始化我的文件,然后解组并从中读取。

当我在将“文件”解组后再将其保存回文本文件之前尝试更新“文件”时,就会出现问题。

isImportStarted 确实有效(删除错误行 obv 时)但我似乎无法正确更新文件我收到此错误:

./test.go:62:34: cannot assign to struct field 
TheList[symbol].ImportStarted in map
./test.go:71:3: cannot take the address of 
TheList[symbol].ImportStarted
./test.go:71:34: cannot assign to &TheList[symbol].ImportStarted

我的代码:

                package main

                import (
                    "encoding/json"
                    "fmt"
                    "os"
                    "io/ioutil"
                    "log"
                )

                type Informations struct {
                        ImportStarted bool
                        ImportDone bool
                }

                var MyList = map[string]*Informations{
                    "test": &Informations{ImportStarted: false,ImportDone:false},
                    "test2": &Informations{ImportStarted: false,ImportDone:false},
                }

                func ReadFile(filename string) []byte{
                    data, err := ioutil.ReadFile(filename)
                    if err != nil {
                        log.Panicf("failed reading data from file: %s", err)
                    }
                        return data
                }

                func writeFile(json string,filename string){
                        file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
                        defer file.Close()

                        if err != nil {
                            fmt.Println(err)
                        }
                        _,err2 := file.WriteString(json)
                        fmt.Println(err2)
                }

                func main() {
                        isImportStarted("test")
                        ImportStart("test")
                }

                func ImportStart(symbol string){
                  filename := "test.txt"
                    _, err := os.Stat(filename)
                    if err != nil {
                        if os.IsNotExist(err) {
                            fmt.Println("File does not exist creating it...")
                            file, err := os.Create(filename)
                            jsonString, _ := json.Marshal(MyList)
                            writeFile(string(jsonString),filename)
                            if err != nil {
                                fmt.Println(err)
                            }
                            fmt.Println("reading from file"+filename )
                            x := ReadFile(filename)

                            var TheList = map[string]Informations{}
                            json.Unmarshal(x,&TheList )
                            TheList[symbol].ImportStarted = true
                            defer file.Close()
                      //wanting to save afterwards...
                        }
                    } else {
                        fmt.Println("reading from file "+ filename)
                        x := ReadFile(filename)
                        var TheList = map[string]Informations{}
                        json.Unmarshal(x,&TheList )
                        &TheList[symbol].ImportStarted = true
                    }
                }


                func isImportStarted(symbol string) bool{
                    filename := "test.txt"
                    x := ReadFile(filename)
                    var TheList = map[string]Informations{}
                    json.Unmarshal(x,&TheList )
                    return TheList[symbol].ImportStarted
                }

我已经尝试过Why do I get a "cannot assign" error when setting value to a struct as a value in a map? 问题,但它根本不适合我的用例,因为它会有效地用 nil 而不是 {false,false} 初始化我的所有结构

有什么想法吗?

【问题讨论】:

    标签: json go marshalling unmarshalling


    【解决方案1】:

    试试var TheList = map[string]*Informations{},为什么不能在map中赋值请参考why-do-i-get-a-cannot-assing-erroraccess-struct-in-map-without-copying

    【讨论】:

      猜你喜欢
      • 2019-12-05
      • 2019-06-06
      • 1970-01-01
      • 2017-12-06
      • 2017-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多