```
// 取redigo 连接
conn := models.Redis(setting.Conf.TimelineRedis).Get()
defer conn.Close()
for i, _ := range times {
// redis key
key := getTimelineKeyByCode(code, times[i])
// 接参数发送命令
err := conn.Send("HMGET", getTimelineArgs(key)...)
if err != nil {
return nil, err
}
}
err := conn.Flush()
if err != nil {
return nil, err
}
list := make([]map[string]string, len(times))
// arr := make([]map[string]string, 0)
for i, _ := range times {
vals, err := redis.Strings(conn.Receive())
if err != nil {
return nil, err
}
info := make(map[string]string)
info = getTimelineVals(vals)
// 没有 数据时
if len(vals[0]) == 0 {
// if i == 0 {
info = emptyInfo
// } else {
// info = arr[i-1]
// }
// } else {
// info = getTimelineVals(vals)
}
info["date"] = times[i]
log.Info("get %d %v", i, info["date"])
list[i] = info
// arr = append(arr, info)
}
for j, in := range arr {
log.Info("set %d %v", j, in["date"])
}
```
说明
times := []string{} 是一个09:29~ 15:00 记录时间分钟数的字符串数组
redis.Strings(conn.Receive()) 前面用redigo 去redis取出了对应时间的数据
getTimelineVals() 把结果 拼成一个map[string]string
问题出在中间 // 没有 数据时
我判断当没有当取到的数据是空字符值,赋给一个默认值
只要在if 条件里加上 info = emptyInfo 就会得到如下结果 ,注释掉就正常
输出结果
```
2016/01/07 21:34:12 [I] get 231 14:50
2016/01/07 21:34:12 [I] get 232 14:51
2016/01/07 21:34:12 [I] get 233 14:52
2016/01/07 21:34:12 [I] get 234 14:53
2016/01/07 21:34:12 [I] get 235 14:54
2016/01/07 21:34:12 [I] get 236 14:55
2016/01/07 21:34:12 [I] get 237 14:56
2016/01/07 21:34:12 [I] get 238 14:57
2016/01/07 21:34:12 [I] get 239 14:58
2016/01/07 21:34:12 [I] get 240 14:59
2016/01/07 21:34:12 [I] get 241 15:00
2016/01/07 21:34:12 [I] set 0 15:00
2016/01/07 21:34:12 [I] set 1 15:00
2016/01/07 21:34:12 [I] set 2 15:00
2016/01/07 21:34:12 [I] set 3 15:00
2016/01/07 21:34:12 [I] set 4 15:00
2016/01/07 21:34:12 [I] set 5 15:00
2016/01/07 21:34:12 [I] set 6 15:00
2016/01/07 21:34:12 [I] set 7 15:00
2016/01/07 21:34:12 [I] set 8 15:00
2016/01/07 21:34:12 [I] set 9 15:00
```
最后打印输出的时间都变成最后一个元素的时间了!!!
部分代码
```
var emptyInfo = map[string]string{
"price": "0",
"high": "0",
"low": "0",
"close": "0",
"vol": "0",
"sum": "0",
}
var timelineFields = []string{
"price",
"high",
"low",
"close",
"vol",
"sum",
}
func getTimelineVals(vals []string) map[string]string {
info := map[string]string{}
for i, _ := range timelineFields {
info[timelineFields[i]] = vals[i]
}
return info
}
```