亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從地圖結構保存讀取/寫入/保存到 json

從地圖結構保存讀取/寫入/保存到 json

Go
湖上湖 2023-06-19 17:15:38
我一直在嘗試擁有一個“工作”文件,我將我的應用程序的某些基本狀態保存到其中,而不是將它們保存在 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")                }我已經嘗試過為什么在將值設置為結構作為映射中的值時出現“無法分配”錯誤?問題,但它根本不適合我的用例,因為它會用 nil 而不是 {false,false} 有效地初始化我所有的結構有任何想法嗎?
查看完整描述

1 回答

?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

嘗試var TheList = map[string]*Informations{},為什么你不能在地圖中賦值。

查看完整回答
反對 回復 2023-06-19
  • 1 回答
  • 0 關注
  • 178 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號