出于某种原因,我无法使用范围进行迭代
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是_FileSystemStore
type FileSystemStore map[string]FileSystemStoreEntry
type FileSystemStoreEntry struct {
FS FileSystem
Stored int64
}
添加data到sessionStore
func StashSession(memfs memory.FileSystem, id string) {
s := sessionStore[id]
s.FS = memfs
s.Stored = time.Now().Unix()
memfs.ReplaceFS(memory.CreateMemoryFileSystem().MFileSystem)
}
阅读data
func 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调用函数
SMILET
相关分类