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


边栏推荐
- 【无标题】
- Yolov3 learning notes
- [untitled] 8 simplified address book
- 【C#/VB.NET】 将PDF转为SVG/Image, SVG/Image转PDF
- Use abp Zero builds a third-party login module (I): Principles
- 代码管理工具
- Condition annotation in uni-app realizes cross segment compatibility, navigation jump and parameter transfer, component creation and use, and life cycle function
- 深入解析kubernetes controller-runtime
- Page text acquisition
- SSH link remote server and local display of remote graphical interface
猜你喜欢

Use abp Zero builds a third-party login module (I): Principles

数值法求解最优控制问题(一)——梯度法

IC_EDA_ALL虚拟机(丰富版):questasim、vivado、vcs、verdi、dc、pt、spyglass、icc2、synplify、INCISIVE、IC617、MMSIM、工艺库

Support vector machine for machine learning

Project summary --2 (basic use of jsup)

Zhiniu stock -- 03

Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)

SQL实现将多行记录合并成一行

Operation principle of lua on C: Foundation

“我为开源打榜狂”第一周榜单公布,160位开发者上榜
随机推荐
堆排序和优先队列
Learning notes -- principles and comparison of k-d tree and IKD tree
opencv
有意思的鼠标指针交互探究
23 design models
opencv鼠标键盘事件
【5G NR】UE注册流程
Unit test framework + Test Suite
Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)
Zhiniu stock project -- 04
[set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
How matlab modifies default settings
Cesium entity (entities) entity deletion method
利用C#实现Pdf转图片
Paper notes vsalm literature review "a comprehensive survey of visual slam algorithms"
Interesting research on mouse pointer interaction
Heap sort and priority queue
100000 bonus is divided up. Come and meet the "sister who braves the wind and waves" among the winners
“我为开源打榜狂”第一周榜单公布,160位开发者上榜
Pdf files can only print out the first page