当前位置:网站首页>golang操作redis:写入、读取kv数据
golang操作redis:写入、读取kv数据
2022-07-03 06:33:00 【学亮编程手记】
代码
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("Set", "name", "tomjerry猫猫")
if err != nil {
fmt.Println("set err=", err)
return
}
//3. 通过go 向redis读取数据 string [key-val]
r, err := redis.String(conn.Do("Get", "name"))
if err != nil {
fmt.Println("set err=", err)
return
}
//因为返回 r是 interface{}
//因为 name 对应的值是string ,因此我们需要转换
//nameString := r.(string)
fmt.Println("操作ok ", r)
}
结果


边栏推荐
- Print time Hahahahahaha
- Interesting research on mouse pointer interaction
- Project summary --2 (basic use of jsup)
- Zhiniu stock project -- 04
- When PHP uses env to obtain file parameters, it gets strings
- Docker advanced learning (container data volume, MySQL installation, dockerfile)
- YOLOV2学习与总结
- Project summary --04
- Kubesphere - build MySQL master-slave replication structure
- 数值法求解最优控制问题(一)——梯度法
猜你喜欢

Example of joint use of ros+pytoch (semantic segmentation)

Important knowledge points of redis

利用C#实现Pdf转图片

Simple understanding of ThreadLocal

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

In depth analysis of kubernetes controller runtime

Click cesium to obtain three-dimensional coordinates (longitude, latitude and elevation)

Docker advanced learning (container data volume, MySQL installation, dockerfile)

JMeter performance automation test

Zhiniu stock -- 03
随机推荐
Yolov3 learning notes
[untitled] 8 simplified address book
In depth analysis of kubernetes controller runtime
Push box games C #
代码管理工具
[untitled] 5 self use history
Click cesium to obtain three-dimensional coordinates (longitude, latitude and elevation)
Use @data in Lombok to simplify entity class code
Yolov1 learning notes
The win7 computer can't start. Turn the CPU fan and stop it
【开源项目推荐-ColugoMum】这群本科生基于国产深度学习框架PaddlePadddle开源了零售行业解决方案
Condition annotation in uni-app realizes cross segment compatibility, navigation jump and parameter transfer, component creation and use, and life cycle function
The mechanical hard disk is connected to the computer through USB and cannot be displayed
Advanced technology management - do you know the whole picture of growth?
Naive Bayes in machine learning
pytorch练习小项目
论文笔记 VSALM 文献综述《A Comprehensive Survey of Visual SLAM Algorithms》
Support vector machine for machine learning
Pdf files can only print out the first page
Important knowledge points of redis