当前位置:网站首页>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)
}
结果


边栏推荐
- Cannot get value with @value, null
- Opencv mouse and keyboard events
- DNS forward query:
- 数值法求解最优控制问题(一)——梯度法
- 代码管理工具
- Zhiniu stock project -- 04
- Scroll view specifies the starting position of the scrolling element
- SQL implementation merges multiple rows of records into one row
- Cesium entity (entities) entity deletion method
- opencv鼠标键盘事件
猜你喜欢

Paper notes vsalm literature review "a comprehensive survey of visual slam algorithms"

Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster

2022年华东师范大学计科考研复试机试题-详细题解

2022 CISP-PTE(三)命令执行

Summary of UI module design and practical application of agent mode

YOLOV1学习笔记

100000 bonus is divided up. Come and meet the "sister who braves the wind and waves" among the winners

Redis cluster creation, capacity expansion and capacity reduction

How to scan when Canon c3120l is a network shared printer

深入解析kubernetes controller-runtime
随机推荐
冒泡排序的简单理解
Zhiniu stock project -- 04
Une exploration intéressante de l'interaction souris - pointeur
23 design models
Yolov2 learning and summary
Heap sort and priority queue
Reinstalling the system displays "setup is applying system settings" stationary
Fluentd facile à utiliser avec le marché des plug - ins rainbond pour une collecte de journaux plus rapide
MATLAB如何修改默认设置
C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
The list of "I'm crazy about open source" was released in the first week, with 160 developers on the list
Chapter 8. MapReduce production experience
Yolov3 learning notes
【无标题】8 简易版通讯录
Project summary --04
2022 CISP-PTE(三)命令执行
[5g NR] UE registration process
Apifix installation
In depth analysis of kubernetes controller runtime
Luogu problem list: [mathematics 1] basic mathematics problems