Update redis link and Update mgo
This commit is contained in:
56
zh/05.6.md
56
zh/05.6.md
@@ -10,10 +10,10 @@ redis是一个key-value存储系统。和Memcached类似,它支持存储的val
|
||||
|
||||
Go目前支持redis的驱动有如下
|
||||
- https://github.com/garyburd/redigo (推荐)
|
||||
- https://github.com/go-redis/redis
|
||||
- https://github.com/hoisie/redis
|
||||
- https://github.com/alphazero/Go-Redis
|
||||
- http://code.google.com/p/tideland-rdc/
|
||||
- https://github.com/simonz05/godis
|
||||
- https://github.com/hoisie/redis.go
|
||||
|
||||
我以redigo驱动为例来演示如何进行数据的操作:
|
||||
```Go
|
||||
@@ -145,6 +145,12 @@ MongoDB是一个高性能,开源,无模式的文档型数据库,是一个
|
||||
|
||||
目前Go支持mongoDB最好的驱动就是[mgo](http://labix.org/mgo),这个驱动目前最有可能成为官方的pkg。
|
||||
|
||||
安装mgo:
|
||||
|
||||
```Go
|
||||
go get gopkg.in/mgo.v2
|
||||
```
|
||||
|
||||
下面我将演示如何通过Go来操作mongoDB:
|
||||
```Go
|
||||
|
||||
@@ -152,38 +158,40 @@ MongoDB是一个高性能,开源,无模式的文档型数据库,是一个
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"labix.org/v2/mgo"
|
||||
"labix.org/v2/mgo/bson"
|
||||
"log"
|
||||
"gopkg.in/mgo.v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
type Person struct {
|
||||
Name string
|
||||
Phone string
|
||||
Name string
|
||||
Phone string
|
||||
}
|
||||
|
||||
func main() {
|
||||
session, err := mgo.Dial("server1.example.com,server2.example.com")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer session.Close()
|
||||
session, err := mgo.Dial("server1.example.com,server2.example.com")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer session.Close()
|
||||
|
||||
session.SetMode(mgo.Monotonic, true)
|
||||
// Optional. Switch the session to a monotonic behavior.
|
||||
session.SetMode(mgo.Monotonic, true)
|
||||
|
||||
c := session.DB("test").C("people")
|
||||
err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
|
||||
&Person{"Cla", "+55 53 8402 8510"})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
c := session.DB("test").C("people")
|
||||
err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
|
||||
&Person{"Cla", "+55 53 8402 8510"})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
result := Person{}
|
||||
err = c.Find(bson.M{"name": "Ale"}).One(&result)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
result := Person{}
|
||||
err = c.Find(bson.M{"name": "Ale"}).One(&result)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("Phone:", result.Phone)
|
||||
fmt.Println("Phone:", result.Phone)
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user