![image.png](https://static.studygolang.com/191006/699f24a746382c73fb175bdd1106d9e4.png)
查看源码是这样的一个结构:上面的代码怎么初始化一个 go 连接池啊
![image.png](https://static.studygolang.com/191006/82e7407598baadc48e97d986083b0b4b.png)
```go
import (
"fmt"
"github.com/garyburd/redigo/redis"
"time"
)
var (
redispool *redis.Pool
)
func init() {
redispool = &redis.Pool{
MaxIdle: 1,
MaxActive: 0,
IdleTimeout: time.Second * 20,
Dial: func() (conn redis.Conn, err error) {
c, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
fmt.Println("get connection error,", err.Error())
return nil, err
}
return c, nil
},
}
}
func main(){
con:=redispool.Get()
defer con.Close()
res,err := redis.String(con.Do("ping"))
if err!=nil{
fmt.Println(err.Error())
return
}
fmt.Println(res)
}
```
#3
更多评论
https://github.com/Gourouting/singo/blob/master/cache/main.go
这个项目可以跑起来,里面有redis,你可以参考参考
#1
https://github.com/eehsiao/go-models-redis/blob/master/redis.go
參考
```
import (
"github.com/go-redis/redis"
)
....
cfg = &redis.Options{
Addr: addr,
Password: pw,
DB: db,
PoolSize : active,
MinIdleConns : idle,
}
db = redis.NewClient(cfg)
```
#2