doulianxing4015 2019-05-30 07:16
浏览 111
已采纳

将读取/写入/保存到Map的Struct中

I've been trying to have a " working " file to which i save certain basic state of my application instead of having them in Ram since they would need to be saved everyday, i've decided on creating file per day, this part is working but i've stripped it from the code for more clarity.

Now i'm able to initialise my file with false value for the informations struct and then unmarshalling and reading from it.

The problem arise when i'm trying to update the "file" after it's been unmarshalled before i save it back to the text file.

The isImportStarted does work (when removing the erronous line obv ) but i can't seem to update the file properly i get this error :

./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

My code :

                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
                }

I've tried the Why do I get a "cannot assign" error when setting value to a struct as a value in a map? question but it doesn't fit my use case at all as it would effectivly initialize all my structs with nil instead of {false,false}

Any ideas?

  • 写回答

1条回答 默认 最新

  • doulao1966 2019-05-30 07:31
    关注

    Try var TheList = map[string]*Informations{}, why you cannot assign a value in a map please refer to why-do-i-get-a-cannot-assing-error or access-struct-in-map-without-copying

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大