我有一個(gè)正在編碼的簡(jiǎn)單結(jié)構(gòu)類(lèi)型。但是,我在解碼數(shù)據(jù)時(shí)做了一些根本錯(cuò)誤的事情。每次我嘗試解碼它時(shí),我都會(huì)收到 EOF 恐慌錯(cuò)誤。//將地圖編碼為gob。將 gob 保存到磁盤(pán)。從磁盤(pán)讀取 gob。將 gob 解碼為另一張地圖。包主import ( "fmt" "encoding/gob" "bytes" "io/ioutil")type hashinfo struct { fname string hash string}func main() { thing := []hashinfo{ {"rahul","test"}, {"boya","test2"}, } m := new(bytes.Buffer) enc := gob.NewEncoder(m) enc.Encode(thing) err := ioutil.WriteFile("gob_data", m.Bytes(), 0600) if err != nil { panic(err) } fmt.Printf("just saved gob with %v\n", thing) n,err := ioutil.ReadFile("gob_data") if err != nil { fmt.Printf("cannot read file") panic(err) } p := bytes.NewBuffer(n) dec := gob.NewDecoder(p) e := []hashinfo{} err = dec.Decode(&e) if err != nil { fmt.Printf("cannot decode") panic(err) } fmt.Printf("just read gob from file and it's showing: %v\n", e)}我創(chuàng)建了 e := []hashinfo{} 對(duì)象以解碼 gobject。我在那里做錯(cuò)了嗎?
無(wú)法解碼 gob 數(shù)據(jù)
寶慕林4294392
2021-07-03 15:00:23