理解Golang
中的[]interface{}
和interface{}
之前在开发Go项目操作Redis时,利用Do函数进行数据操作,在返回的interface{}
类型的转换中踩了一个大坑。
Do(ctx, "HKEYS", "KEY")
在阅读源码中发现,Do方法的是一个[]interface{}
切片
func (c *Redis) Do(ctx context.Context, commandName string, args ...interface{
}) (interface{
}, error) {
//
// ...
//
reply, err := conn.Do(commandName, args...)
//
// ...
//
return reply, c.err
}
func (c *conn) Do(cmd string, args ...interface{
}) (interface{
}, error) {
return c.DoWithTimeout(c.readTimeout, cmd, args...)
}
func (c *conn) DoWithTimeout(readTimeout time.Duration, cmd string, args ...interface{
}