出于某种原因,我无法使用范围进行迭代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) }}sessionStoretype是_FileSystemStoretype FileSystemStore map[string]FileSystemStoreEntrytype FileSystemStoreEntry struct { FS FileSystem Stored int64}添加data到sessionStorefunc StashSession(memfs memory.FileSystem, id string) { s := sessionStore[id] s.FS = memfs s.Stored = time.Now().Unix() memfs.ReplaceFS(memory.CreateMemoryFileSystem().MFileSystem)}阅读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)}我可以read和write数据sessionStore但不能迭代它。当我调用LsSessionsCommand()输出时,我得到id stored time 它只是永远不会到达范围函数的主体。我先调用StashSession函数,然后LsSessionsCommand调用函数
1 回答
SMILET
TA贡献1796条经验 获得超4个赞
我看到的是您的保存逻辑有问题,我怀疑您无法迭代地图,因为它实际上是空的。
这里
func StashSession(memfs memory.FileSystem, id string) {
s := sessionStore[id]
s.FS = memfs
s.Stored = time.Now().Unix()
//...
}
您没有在地图中添加新元素。为此,您需要:
sessionStore[id] = FileSystemStoreEntry{
FS: memfs,
Stored: time.Now().Unix(),
//....
}
- 1 回答
- 0 关注
- 90 浏览
添加回答
举报
0/150
提交
取消