level 1
jxdz232601
楼主
代码如下:
type Map_bytes struct {
In_bytes uint64
Out_bytes uint64
Total_bytes uint64
}
users := make(map[string]Map_bytes
_, ok := users[thiskey]
if ok == false {
var tmpu mystore.Map_bytes
tmpu.In_bytes = 0;
tmpu.Out_bytes = 0;
tmpu.Total_bytes = 0;
users1[thiskey] = tmpu
}
users[thiskey].In_bytes += In_bytes
users[thiskey].Out_bytes += Out_bytes
users[thiskey].Total_bytes += Total_bytes
上面就是大致过程,内容就是判断thiskey是否存在,不存在就初始化,存在就直接将值累加。
但是报错了:
./dpiserver.go:138:29: invalid operation: cannot index users1[thiskey] (map index expression of type Map_bytes)
./dpiserver.go:139:30: invalid operation: cannot index users1[thiskey] (map index expression of type Map_bytes)
./dpiserver.go:140:32: invalid operation: cannot index users1[thiskey] (map index expression of type Map_bytes)
不知道是什么问题,上面的操作在其他语言是常规操作,但是这里报错了,是我语法有问题,还是不允许这么做?
最终我用了个本办法,先将tmpu := users1[thiskey] ,再tmpu.In_bytes += In_bytes,最后将tmpu重新赋给users1[thiskey],这么做,貌似有点绕了。
2024年01月23日 09点01分
1
type Map_bytes struct {
In_bytes uint64
Out_bytes uint64
Total_bytes uint64
}
users := make(map[string]Map_bytes
_, ok := users[thiskey]
if ok == false {
var tmpu mystore.Map_bytes
tmpu.In_bytes = 0;
tmpu.Out_bytes = 0;
tmpu.Total_bytes = 0;
users1[thiskey] = tmpu
}
users[thiskey].In_bytes += In_bytes
users[thiskey].Out_bytes += Out_bytes
users[thiskey].Total_bytes += Total_bytes
上面就是大致过程,内容就是判断thiskey是否存在,不存在就初始化,存在就直接将值累加。
但是报错了:
./dpiserver.go:138:29: invalid operation: cannot index users1[thiskey] (map index expression of type Map_bytes)
./dpiserver.go:139:30: invalid operation: cannot index users1[thiskey] (map index expression of type Map_bytes)
./dpiserver.go:140:32: invalid operation: cannot index users1[thiskey] (map index expression of type Map_bytes)
不知道是什么问题,上面的操作在其他语言是常规操作,但是这里报错了,是我语法有问题,还是不允许这么做?
最终我用了个本办法,先将tmpu := users1[thiskey] ,再tmpu.In_bytes += In_bytes,最后将tmpu重新赋给users1[thiskey],这么做,貌似有点绕了。