I am writing a simple Go web application with Redis (trying out redis for the first time) on Windows. I'm using the go-redis package to connect to Redis.
我正在Windows上用Redis(第一次尝试Redis)编写一个简单的Go Web应用程序。我正在使用Go-redis程序包连接到Redis。
package main
import (
"fmt"
"net/http"
"text/template"
"github.com/go-redis/redis"
"github.com/gorilla/mux"
)
var client *redis.Client
var tmpl *template.Template
func init() {
client = redis.NewClient(&redis.Options{
Addr: "localhost:6397",
Password: "",
DB: 0,
})
tmpl = template.Must(template.ParseGlob("./templates/*.gohtml"))
pong, err := client.Ping().Result()
fmt.Println(pong, err)
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/", indexHandler).Methods("GET")
http.Handle("/", router)
http.ListenAndServe(":1234", nil)
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
comments, err := client.LRange("comments", 0, 10).Result()
check(err)
tmpl.ExecuteTemplate(w, "index.gohtml", comments)
}
func check(err error) {
if err != nil {
fmt.Println(err)
return
}
}
But when I run this code, I get
但当我运行这段代码时,我得到
dial tcp [::1]:6397: connectex: No connection could be made because the target machine actively refused it.
The only answer that I could find was to "start the redis server". My redis server is up and running (Checked it by using the "PING" command in redis client). I have also tried running it as administrator, but no luck.
我能找到的唯一答案是“启动Redis服务器”。我的redis服务器已经启动并运行(在redis客户端使用“ping”命令进行检查)。我也尝试以管理员身份运行它,但没有成功。
Screenshot:
屏幕截图:
更多回答
优秀答案推荐
This is happening most likely because of the Redis server is running on port 6379
(that is the default port for Redis server) but you are trying to connect to the port 6397
.
这很可能是因为Redis服务器运行在端口6379(这是Redis服务器的默认端口)上,而您正在尝试连接到端口6397。
Change the Server Address to:
将服务器地址更改为:
Addr: "localhost:6379"
地址:“本地主机:6379”
From
Addr: "localhost:6397"
发件人地址:“本地主机:6397”
And that should solve your problem.
这应该会解决你的问题。
更多回答
Thank you so much, that fixed it! Really embarrassed though, considering how silly my problem was. Thanks again!
太感谢你了,把它修好了!不过,考虑到我的问题有多傻,真的很尴尬。再次感谢!
It happens, no problem.
这种事时有发生,没问题。
我是一名优秀的程序员,十分优秀!