使用地图参考访问地图 [英] Accessing a map using its reference
本文介绍了使用地图参考访问地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试遍历一个映射,将其作为指向函数的指针传递,但是我找不到访问元素的方法.这是代码:
I try to loop through a map, that I pass as a pointer to a function, but I can't find a way to access the elements. This is the code:
func refreshSession(sessions *map[string]Session) {
now := time.Now()
for sid := range *sessions {
if now.After(*sessions[sid].timestamp.Add(sessionRefresh)) {
delete( *sessions, sid )
}
}
}
此示例中的第4行返回以下编译错误:
Line 4 in this example return following compile error:
./controller.go:120: invalid operation: sessions[sid] (type *map[string]Session does not support indexing)
我尝试了方括号,但是没有效果.如果我带走了所有引用运算符(*&),那么它可以正常编译.
I tried brackets, but it had no effect. If I take away all reference operators (* &) then it compiles fine.
我该怎么写?
推荐答案
您不需要在地图上使用指针.
You don't need to use a pointer with a map.
如果需要更改Session
,则可以使用指针:
If you needed to change the Session
you could use a pointer:
map[string]*Session
这篇关于使用地图参考访问地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文