Conversation
func Do need a interface{} slice, not a string and a []interface{}. If input k1,f1,f2, hmget get [k1, [f1, f2]], this is not work, hmget need [k1, f1, f2].
After test, append(args, hashKeys) will return [string, []string]. So only can use for loop to get [string, string, string...]
connection.go
Outdated
|
|
||
| func HMGet(RConn *redigo.Conn, key string, hashKeys ...string) ([]interface{}, error) { | ||
| args := []interface{}{key} | ||
| args = append(args, hashKeys) |
There was a problem hiding this comment.
Have you tried this? - args = append(args, hasKeys...)
There was a problem hiding this comment.
of course, but cannot use hasKeys(type []string) as type []interface{}, unless change the hasKeys to ...interface{}
There was a problem hiding this comment.
func HMGet1(key string, hashKeys ...string) {
args := []interface{}{key}
for _, v := range hashKeys {
args = append(args, v)
}
}
func HMGet2(key string, hashKeys ...string) {
args := make([]interface{}, 1+len(hashKeys))
args[0] = key
for i, v := range hashKeys {
args[i+1] = v
}
}
func HMGet3(key string, hashKeys ...interface{}) {
args := []interface{}{key}
args = append(args, hashKeys...)
}
3000000 602 ns/op
3000000 417 ns/op
5000000 259 ns/op
I bench test the three method make a string and []string to a []string. The HMGet3 is best, so i suggest modifying the function parameter type to ...interface{}.
like this
func HMGet(RConn *redigo.Conn, key string, hashKeys ...interface{})
func HMGet1(key string, hashKeys ...string) {
args := []interface{}{key}
for _, v := range hashKeys {
args = append(args, v)
}
}
func HMGet2(key string, hashKeys ...string) {
args := make([]interface{}, 1+len(hashKeys))
args[0] = key
for i, v := range hashKeys {
args[i+1] = v
}
}
func HMGet3(key string, hashKeys ...interface{}) {
args := []interface{}{key}
args = append(args, hashKeys...)
}
3000000 602 ns/op
3000000 417 ns/op
5000000 259 ns/op
HMGet3 is best
After test, append(args, hashKeys) will return [string, []string]. So only can use ‘for loop‘ to get [string, string, string...], then hmget can get the well return