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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配