由于某種原因,我無(wú)法用范圍迭代var sessionStore = make(FileSystemStore)func LsSessionsCommand(_ []string, _ *string, _ *memory.FileSystem){ w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0) fmt.Fprint(w, "id\tstored time\n") for key, val := range sessionStore { // never reaches here t := time.Unix(val.Stored, 0) createdTime := fmt.Sprintf("%d:%d %d/%d/%d", t.Hour(), t.Minute(), t.Day(), t.Month(), t.Year()) _, err := fmt.Fprintf(w, "%s\t%s\n", key, createdTime) if err != nil { fmt.Printf("unable to list sessions: %v", err) } } err := w.Flush() if err != nil { fmt.Printf("unable to list sessions: %v", err) }}sessionStore的typeFileSystemStoretype FileSystemStore map[string]FileSystemStoreEntrytype FileSystemStoreEntry struct { FS FileSystem Stored int64}添加到datasessionStorefunc StashSession(memfs memory.FileSystem, id string) { s := sessionStore[id] s.FS = memfs s.Stored = time.Now().Unix() memfs.ReplaceFS(memory.CreateMemoryFileSystem().MFileSystem)}讀數(shù)datafunc CollectSession(memfs memory.FileSystem, id string, stashCurrent bool, newid string) { s := sessionStore[id] fs := s.FS.MFileSystem if stashCurrent { s.FS = memfs } memfs.ReplaceFS(fs)}我可以和數(shù)據(jù),但不能迭代它。當(dāng)我調(diào)用輸出時(shí),我得到它只是永遠(yuǎn)不會(huì)到達(dá)范圍函數(shù)的主體。readwritesessionStoreLsSessionsCommand()id stored time我先叫函數(shù),再叫函數(shù)StashSessionLsSessionsCommand
不能在 golang 中使用范圍
寶慕林4294392
2022-08-01 10:24:15