假设如下
type User struct {
name string
}
users := make(map[int]User)
users[5] = User{"Steve"}
Run Code Online (Sandbox Code Playgroud)
为什么不能访问现在存储在地图中的struct实例?
users[5].name = "Mark"
Run Code Online (Sandbox Code Playgroud)
任何人都可以了解如何访问地图存储的结构,或者为什么它不可能的逻辑?
我知道你可以通过制作结构的副本,更改副本和复制回地图来实现这一目标 - 但这是一个代价高昂的复制操作.
我也知道这可以通过在我的地图中存储struct指针来完成,但我也不想这样做.
我正在尝试对需要昂贵准备的操作进行基准测试,但我不使用 StopTimer() 和 StartTimer()。具体来说,我正在将第 n 个项目插入到排序列表中进行基准测试。
示例代码:
n := 100
// Run the process b.N times
for i := 0; i < b.N; i++ {
// Stop the timer for our expensive preparation work (inserting n-1 items)
b.StopTimer()
// ...
// Insert n-1 items
for j := 1; j < n; j++ {
m.InsertItem(o)
}
// ...
// Resume the timer
b.StartTimer()
// Insert the nth item
m.InsertItem(o)
}
Run Code Online (Sandbox Code Playgroud)
问题是 Go 的基准测试启发式是根据基准时间而不是总时间来限制 bN。它最终要求第 100 个订单插入的 5MM(5000000)次迭代,这需要比合理的更多时间(我想对第 10 百万个项目插入进行基准测试)。
有没有办法在 Go …
go ×2