我一直在嘗試擁有一個“工作”文件,我將我的應用程序的某些基本狀態保存到其中,而不是將它們保存在 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 回答
- 0 關注
- 178 瀏覽
添加回答
舉報
0/150
提交
取消