Update redis link and Update mgo
This commit is contained in:
75
en/05.6.md
75
en/05.6.md
@@ -10,10 +10,10 @@ redis is a key-value storage system like Memcached, that supports the string, li
|
||||
|
||||
There are some Go database drivers for redis:
|
||||
- [https://github.com/garyburd/redigo](https://github.com/garyburd/redigo)
|
||||
- [https://github.com/go-redis/redis](https://github.com/go-redis/redis)
|
||||
- [https://github.com/hoisie/redis](https://github.com/hoisie/redis)
|
||||
- [https://github.com/alphazero/Go-Redis](https://github.com/alphazero/Go-Redis)
|
||||
- [http://code.google.com/p/tideland-rdc/](http://code.google.com/p/tideland-rdc/)
|
||||
- [https://github.com/simonz05/godis](https://github.com/simonz05/godis)
|
||||
- [https://github.com/hoisie/redis.go](https://github.com/hoisie/redis.go)
|
||||
|
||||
Let's see how to use the driver that redigo to operate on a database:
|
||||
```Go
|
||||
@@ -142,46 +142,57 @@ Figure 5.1 MongoDB compared to Mysql
|
||||
|
||||
The best driver for mongoDB is called `mgo`, and it is possible that it will be included in the standard library in the future.
|
||||
|
||||
Install mgo:
|
||||
|
||||
```Go
|
||||
go get gopkg.in/mgo.v2
|
||||
```
|
||||
|
||||
Here is the example:
|
||||
```Go
|
||||
|
||||
package main
|
||||
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"labix.org/v2/mgo"
|
||||
"labix.org/v2/mgo/bson"
|
||||
"fmt"
|
||||
"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.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)
|
||||
}
|
||||
|
||||
result := Person{}
|
||||
err = c.Find(bson.M{"name": "Ale"}).One(&result)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("Phone:", result.Phone)
|
||||
session, err := mgo.Dial("server1.example.com,server2.example.com")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer session.Close()
|
||||
|
||||
// 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 {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
result := Person{}
|
||||
err = c.Find(bson.M{"name": "Ale"}).One(&result)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("Phone:", result.Phone)
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
We can see that there are no big differences when it comes to operating on mgo or beedb databases; they are both based on structs. This is the Go way of doing things.
|
||||
|
||||
## Links
|
||||
|
||||
Reference in New Issue
Block a user