Redis connection with go lang

huangapple go评论77阅读模式
英文:

Redis connection with go lang

问题

我在尝试使用Go语言连接Redis时遇到了"dial: unknown network error"错误。代码如下:

var client *redis.Client

client = redis.NewClient(&redis.Options{
    Addr:     "localhost:6379",
    Password: "",
    DB:       0,
})

请问为什么会出现这个错误?请帮忙解决。

英文:

I am getting

> dial : unknown network error

when i try to connect redis in go lang like this :

var client *redis.Client

client = redis.NewClient(&redis.Options{
	Addr : "localhost:6379",
	Password:"",
	DB       : 0,
});

Why i am getting this error,please help.

答案1

得分: 6

你没有为Dialer提供网络参数:

client := redis.NewClient(&redis.Options{
	Network:  "tcp",
	Addr:     "localhost:6379",
	Password: "",
	DB:       0,
})

或者使用NewTCPClient,它会为你设置"tcp"网络参数。

英文:

You're not providing a network parameter for the Dialer:

client := redis.NewClient(&redis.Options{
	Network:  "tcp",
	Addr:     "localhost:6379",
	Password: "",
	DB:       0,
})

Or use NewTCPClient, which sets the "tcp" network parameter for you.

huangapple
  • 本文由 发表于 2017年1月4日 04:27:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/41451787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定