当前位置:网站首页>golang操作redis:写入、读取hash类型数据
golang操作redis:写入、读取hash类型数据
2022-07-03 06:33:00 【学亮编程手记】
golang操作redis:写入、读取hash类型数据
代码
package main
import (
"fmt"
"github.com/garyburd/redigo/redis" //引入redis包
)
func main() {
//通过go 向redis 写入数据和读取数据
//1. 链接到redis
conn, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
fmt.Println("redis.Dial err=", err)
return
}
defer conn.Close() //关闭..
//2. 通过go 向redis写入数据 string [key-val]
_, err = conn.Do("HSet", "user01", "name", "john")
if err != nil {
fmt.Println("hset err=", err)
return
}
_, err = conn.Do("HSet", "user01", "age", 18)
if err != nil {
fmt.Println("hset err=", err)
return
}
//3. 通过go 向redis读取数据
r1, err := redis.String(conn.Do("HGet","user01", "name"))
if err != nil {
fmt.Println("hget err=", err)
return
}
r2, err := redis.Int(conn.Do("HGet","user01", "age"))
if err != nil {
fmt.Println("hget err=", err)
return
}
//因为返回 r是 interface{}
//因为 name 对应的值是string ,因此我们需要转换
//nameString := r.(string)
fmt.Printf("操作ok r1=%v r2=%v \n", r1, r2)
}
结果
边栏推荐
- Condition annotation in uni-app realizes cross segment compatibility, navigation jump and parameter transfer, component creation and use, and life cycle function
- Zhiniu stock project -- 04
- Docker advanced learning (container data volume, MySQL installation, dockerfile)
- Kubesphere - set up redis cluster
- Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)
- [system design] proximity service
- Various usages of MySQL backup database to create table select and how many days are left
- Operation principle of lua on C: Foundation
- Simple understanding of bubble sorting
- 使用 Abp.Zero 搭建第三方登录模块(一):原理篇
猜你喜欢
23 design models
Creating postgre enterprise database by ArcGIS
第8章、MapReduce 生产经验
YOLOV3学习笔记
Kubesphere - build Nacos cluster
Local rviz call and display of remote rostopic
Yolov2 learning and summary
[system design] proximity service
Use selenium to climb the annual box office of Yien
剖析虚幻渲染体系(16)- 图形驱动的秘密
随机推荐
Opencv mouse and keyboard events
【无标题】
Decision tree of machine learning
Use selenium to climb the annual box office of Yien
The mechanical hard disk is connected to the computer through USB and cannot be displayed
C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
Luogu problem list: [mathematics 1] basic mathematics problems
Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
Page text acquisition
致即将毕业大学生的一封信
Use abp Zero builds a third-party login module (I): Principles
Derivation of variance iteration formula
JMeter performance automation test
Some thoughts on machine learning
【开源项目推荐-ColugoMum】这群本科生基于国产深度学习框架PaddlePadddle开源了零售行业解决方案
Print time Hahahahahaha
Openresty best practices
ssh链接远程服务器 及 远程图形化界面的本地显示
Printer related problem record
Floating menu operation