2 回答

TA贡献1802条经验 获得超10个赞
解决方案是使用 getter 函数。
我们将所有非指针字段隔离在一个名为barPublicinstance 的结构中。该函数getByID将返回此结构的副本。为了从 的映射中访问值bar,我们使用专门的 getter 函数getByIDAndKey。
// struct containing non pointer fields of bar
type barPublic struct {
x string
}
type bar struct {
barPublic
largeMap map[int]int
}
var cache map[int]bar
func getByID(id int) (barPublic, bool) {
v, ok := cache[id]
if !ok {
return barPublic{}, false
}
return v.barPublic, true
}
func getByIDAndKey(id, key int) int {
v, ok := cache[id]
if !ok {
return 0, false
}
w, ok := v.largeMap[key]
if !ok {
return 0, false
}
return w, true
}
- 2 回答
- 0 关注
- 120 浏览
添加回答
举报