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


边栏推荐
- 冒泡排序的简单理解
- [set theory] equivalence relation (concept of equivalence relation | examples of equivalence relation | equivalence relation and closure)
- 使用conda创建自己的深度学习环境
- Simple password lock
- Yolov3 learning notes
- Simple understanding of bubble sorting
- Summary of UI module design and practical application of agent mode
- 代码管理工具
- Local rviz call and display of remote rostopic
- Svn branch management
猜你喜欢

JMeter performance automation test

Install VM tools

10万奖金被瓜分,快来认识这位上榜者里的“乘风破浪的姐姐”

Chapter 8. MapReduce production experience

使用conda创建自己的深度学习环境

Kubesphere - build Nacos cluster

利用C#实现Pdf转图片

ssh链接远程服务器 及 远程图形化界面的本地显示

Operation principle of lua on C: Foundation

100000 bonus is divided up. Come and meet the "sister who braves the wind and waves" among the winners
随机推荐
Apifix installation
Time format record
Paper notes vsalm literature review "a comprehensive survey of visual slam algorithms"
Cesium entity (entities) entity deletion method
Pytest attempts to execute the test case without skipping, but the case shows that it is all skipped
opencv
Fluentd facile à utiliser avec le marché des plug - ins rainbond pour une collecte de journaux plus rapide
Climb movie paradise 2021 hot
[open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle
[C /vb.net] convert PDF to svg/image, svg/image to PDF
Judge whether the date time exceeds 31 days
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
Openresty best practices
学习笔记 -- k-d tree 和 ikd-Tree 原理及对比
【无标题】
致即将毕业大学生的一封信
简易密码锁
【C#/VB.NET】 将PDF转为SVG/Image, SVG/Image转PDF
Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)
. Net program configuration file operation (INI, CFG, config)