import (
"fmt"
"github.com/garyburd/redigo/redis"
)
/*
hash的一些基本操作:
hset hash field value 将哈希表hash中域 field 的值设置为 value
如果哈希表不存在, 那么将创建并写入, 如果存在则更新hash表中的数据
eg:
127.0.0.1:6379> hset website google www.g.cn
(integer) 1
127.0.0.1:6379> hget website google
"www.g.cn"
hsetnx hash field value 当且仅当域field不存在于哈希表中的时候,将值设置为value
如果表不存在 则创建并执行写入field value
eg:
127.0.0.1:6379> hsetnx database nosql redis
(integer) 1
127.0.0.1:6379> hsetnx database nosql mongo
(integer) 0
127.0.0.1:6379> hget database nosql
"redis"
hget hash field 返回哈希表中给定的域值
eg:
127.0.0.1:6379> hget database nosql
"redis"
127.0.0.1:6379> hget data redis
(nil)
hexists hash field 检查field是否在hash表中
eg:
127.0.0.1:6379> hexists database nosql
(integer) 1
127.0.0.1:6379> hexists database redis
(integer) 0
hdel hash field ... 删除哈希表hash中的一个或多个指定区域,不存在的域将被忽略。
eg:
127.0.0.1:6379> hset database sql mysql
(integer) 1
127.0.0.1:6379> hdel database sql
(integer) 1
127.0.0.1:6379> hdel database sql nosql
(integer) 1
hlen key 返回哈希表key中域的数量
eg:
127.0.0.1:6379> hlen database
(integer) 0
hstrlen key field 返回哈希表key中制定field的字符串长度
eg:
127.0.0.1:6379> hset database sql mysql
(integer) 1
127.0.0.1:6379> hstrlen database sql
(integer) 5
hincrby key field increment 为哈希表 key 中的域 field 的值加上增量 increment
不能对字符串域field执行hincrby命令
eg:
127.0.0.1:6379> hset add one 1
(integer) 1
127.0.0.1:6379> hincrby add one 1000
(integer) 1001
hincrbyfloat key field increment 为哈希表key中的域field加上浮点数增量increment
eg:
127.0.0.1:6379> hincrby add one 1000
(integer) 1001
127.0.0.1:6379> hincrbyfloat add one 99.555555
"1100.55555500000000002"
hmset key field value [field value …] 同时将多个field-value(域-值)对设置到哈希表 key 中。
eg:
127.0.0.1:6379> hmset fruit key1 apple key2 banana key3 orange
OK
hmget key field [field …] 返回哈希表 key 中,一个或多个给定域的值。
eg:
127.0.0.1:6379> hmget fruit key1 key2 key3
1) "apple"
2) "banana"
3) "orange"
hkeys key 返回哈希表 key 中的所有域。
eg:
127.0.0.1:6379> hkeys fruit
1) "key1"
2) "key2"
3) "key3"
hvals key 返回哈希表key中所有域的值
eg:
127.0.0.1:6379> hvals fruit
1) "apple"
2) "banana"
3) "orange"
hgetall key 返回哈希表 key 中,所有的域和值。
eg:
127.0.0.1:6379> hgetall fruit
1) "key1"
2) "apple"
3) "key2"
4) "banana"
5) "key3"
6) "orange"
*/
func main() {
conn, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
fmt.Println("conn error: ", err)
}
defer conn.Close()
// hset
_, err = conn.Do("hset", "website", "google", "www.google.com")
if err != nil {
fmt.Println("hset error : ", err)
}
// hmset
_, err = conn.Do("hmset", "website", "baidu", "www.baidu.com", "qq", "www.qq.com")
if err != nil {
fmt.Println("hmset error:", err)
}
// hgetall
res, err := redis.StringMap(conn.Do("hgetall", "website"))
if err == nil {
fmt.Println(res)
}else {
fmt.Println("hgetall error: ", err)
}
// hvals
rs, err := redis.Strings(conn.Do("hvals", "website"))
if err == nil {
fmt.Println(rs)
}else {
fmt.Println(err)
}
// hkeys
rk, err := redis.Strings(conn.Do("hkeys", "website"))
if err == nil {
fmt.Println(rk)
}else {
fmt.Println(err)
}
// hstrlen
l, err := redis.Int(conn.Do("hstrlen", "website", "google"))
if err == nil {
fmt.Println(l)
}else {
fmt.Println(err)
}
// hlen
l, err = redis.Int(conn.Do("hlen", "website"))
if err == nil {
fmt.Println(l)
}else {
fmt.Println(err)
}
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- TiDB入门(四):从入门到“跑路”
- MyBatis从入门到精通(一):MyBatis入门
- MyBatis从入门到精通(一):MyBatis入门
- Docker入门(一)用hello world入门docker
- 赵童鞋带你入门PHP(六) ThinkPHP框架入门
- 初学者入门 Golang 的学习型项目,go入门项目
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Shallows
Nicholas Carr / W. W. Norton & Company / 2011-6-6 / USD 15.95
"Is Google making us stupid?" When Nicholas Carr posed that question, in a celebrated Atlantic Monthly cover story, he tapped into a well of anxiety about how the Internet is changing us. He also crys......一起来看看 《The Shallows》 这本书的介绍吧!