Merge pull request #773 from vCaesar/u5-pr

Update preface word and Update mgo
This commit is contained in:
astaxie
2017-01-08 21:28:03 +08:00
committed by GitHub
9 changed files with 174 additions and 134 deletions

View File

@@ -66,44 +66,49 @@ The best driver for mongoDB is called `mgo`, and it is possible that it will be
Here is the example: Here is the example:
```Go
package main package main
import ( import (
"fmt" "fmt"
"labix.org/v2/mgo" "gopkg.in/mgo.v2"
"labix.org/v2/mgo/bson" "gopkg.in/mgo.v2/bson"
"log"
) )
type Person struct { type Person struct {
Name string Name string
Phone 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)
} }
func main() {
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. 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 ## Links

View File

@@ -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: There are some Go database drivers for redis:
- [https://github.com/garyburd/redigo](https://github.com/garyburd/redigo) - [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) - [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/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: Let's see how to use the driver that redigo to operate on a database:
```Go ```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. 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: Here is the example:
```Go
package main package main
import ( import (
"fmt" "fmt"
"labix.org/v2/mgo" "gopkg.in/mgo.v2"
"labix.org/v2/mgo/bson" "gopkg.in/mgo.v2/bson"
"log"
) )
type Person struct { type Person struct {
Name string Name string
Phone string Phone string
} }
func main() { func main() {
session, err := mgo.Dial("server1.example.com,server2.example.com") session, err := mgo.Dial("server1.example.com,server2.example.com")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer session.Close() 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"}, c := session.DB("test").C("people")
&Person{"Cla", "+55 53 8402 8510"}) err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
if err != nil { &Person{"Cla", "+55 53 8402 8510"})
panic(err) if err != nil {
} log.Fatal(err)
}
result := Person{}
err = c.Find(bson.M{"name": "Ale"}).One(&result) result := Person{}
if err != nil { err = c.Find(bson.M{"name": "Ale"}).One(&result)
panic(err) if err != nil {
} log.Fatal(err)
}
fmt.Println("Phone:", result.Phone)
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. 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 ## Links

View File

@@ -65,44 +65,48 @@ Figura 5.1 MongoDB comparada con Mysql
El mejor manejador para mongoDB es llamado `mgo`, y es posible que se incluya en la librería estándar en el futuro. El mejor manejador para mongoDB es llamado `mgo`, y es posible que se incluya en la librería estándar en el futuro.
Aquí está un ejemplo Aquí está un ejemplo
``` ```Go
package main package main
import ( import (
"fmt" "fmt"
"labix.org/v2/mgo" "gopkg.in/mgo.v2"
"labix.org/v2/mgo/bson" "gopkg.in/mgo.v2/bson"
"log"
) )
type Person struct { type Person struct {
Name string Name string
Phone string Phone string
} }
func main() { func main() {
session, err := mgo.Dial("server1.example.com,server2.example.com") session, err := mgo.Dial("server1.example.com,server2.example.com")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer session.Close() 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") c := session.DB("test").C("people")
err = c.Insert(&Person{"Ale", "+55 53 8116 9639"}, err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
&Person{"Cla", "+55 53 8402 8510"}) &Person{"Cla", "+55 53 8402 8510"})
if err != nil { if err != nil {
panic(err) log.Fatal(err)
} }
result := Person{} result := Person{}
err = c.Find(bson.M{"name": "Ale"}).One(&result) err = c.Find(bson.M{"name": "Ale"}).One(&result)
if err != nil { if err != nil {
panic(err) log.Fatal(err)
} }
fmt.Println("Phone:", result.Phone) fmt.Println("Phone:", result.Phone)
} }
``` ```
Como podemos ver no hay muchas diferencias en lo que respecta a operar con mgo o bases de datos beedb; ambas son basadas en estructuras. Esta es la manera en que Go hace las cosas. Como podemos ver no hay muchas diferencias en lo que respecta a operar con mgo o bases de datos beedb; ambas son basadas en estructuras. Esta es la manera en que Go hace las cosas.

View File

@@ -66,16 +66,19 @@ MongoDBは高性能でオープンソース、モードレスなドキュメン
次にどのようにしてGoからmongoDBを操作するのかご説明します 次にどのようにしてGoからmongoDBを操作するのかご説明します
```Go
package main package main
import ( import (
"fmt" "fmt"
"labix.org/v2/mgo" "gopkg.in/mgo.v2"
"labix.org/v2/mgo/bson" "gopkg.in/mgo.v2/bson"
"log"
) )
type Person struct { type Person struct {
Name string Name string
Phone string Phone string
} }
@@ -86,24 +89,27 @@ MongoDBは高性能でオープンソース、モードレスなドキュメン
} }
defer session.Close() defer session.Close()
// Optional. Switch the session to a monotonic behavior.
session.SetMode(mgo.Monotonic, true) session.SetMode(mgo.Monotonic, true)
c := session.DB("test").C("people") c := session.DB("test").C("people")
err = c.Insert(&Person{"Ale", "+55 53 8116 9639"}, err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
&Person{"Cla", "+55 53 8402 8510"}) &Person{"Cla", "+55 53 8402 8510"})
if err != nil { if err != nil {
panic(err) log.Fatal(err)
} }
result := Person{} result := Person{}
err = c.Find(bson.M{"name": "Ale"}).One(&result) err = c.Find(bson.M{"name": "Ale"}).One(&result)
if err != nil { if err != nil {
panic(err) log.Fatal(err)
} }
fmt.Println("Phone:", result.Phone) fmt.Println("Phone:", result.Phone)
} }
```
mgoの操作方法とbeedbの操作方法はほとんど似ていることがわかります。どちらもstructに基づいて操作する方法です。これこそがGo Styleです。 mgoの操作方法とbeedbの操作方法はほとんど似ていることがわかります。どちらもstructに基づいて操作する方法です。これこそがGo Styleです。

View File

@@ -66,44 +66,50 @@ The best driver for mongoDB is called `mgo`, and it is possible that it will be
Here is the example: Here is the example:
```Go
package main package main
import ( import (
"fmt" "fmt"
"labix.org/v2/mgo" "gopkg.in/mgo.v2"
"labix.org/v2/mgo/bson" "gopkg.in/mgo.v2/bson"
"log"
) )
type Person struct { type Person struct {
Name string Name string
Phone string Phone string
} }
func main() { func main() {
session, err := mgo.Dial("server1.example.com,server2.example.com") session, err := mgo.Dial("server1.example.com,server2.example.com")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer session.Close() 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"}, c := session.DB("test").C("people")
&Person{"Cla", "+55 53 8402 8510"}) err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
if err != nil { &Person{"Cla", "+55 53 8402 8510"})
panic(err) if err != nil {
} log.Fatal(err)
}
result := Person{}
err = c.Find(bson.M{"name": "Ale"}).One(&result) result := Person{}
if err != nil { err = c.Find(bson.M{"name": "Ale"}).One(&result)
panic(err) if err != nil {
} log.Fatal(err)
}
fmt.Println("Phone:", result.Phone)
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. 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 ## Links

View File

@@ -20,4 +20,4 @@ Go是一种编译型语言它结合了解释型语言的游刃有余动态
## links ## links
* [目录](<preface.md>) * [目录](<preface.md>)
* 下一节: [Go安装](<01.1.md>) * 下一节: [安装Go](<01.1.md>)

View File

@@ -176,5 +176,5 @@ go get本质上可以理解为首先第一步是通过源码工具clone代码到
## links ## links
* [目录](<preface.md>) * [目录](<preface.md>)
* 上一节: [GO安装](<01.1.md>) * 上一节: [安装Go](<01.1.md>)
* 下一节: [GO 命令](<01.3.md>) * 下一节: [GO 命令](<01.3.md>)

View File

@@ -131,4 +131,4 @@ Go实现的支持PostgreSQL的驱动也很多因为国外很多人在开发
## links ## links
* [目录](<preface.md>) * [目录](<preface.md>)
* 上一节: [使用SQLite数据库](<05.3.md>) * 上一节: [使用SQLite数据库](<05.3.md>)
* 下一节: [使用beedb库进行ORM开发](<05.5.md>) * 下一节: [使用Beego orm库进行ORM开发](<05.5.md>)

View File

@@ -10,10 +10,10 @@ redis是一个key-value存储系统。和Memcached类似它支持存储的val
Go目前支持redis的驱动有如下 Go目前支持redis的驱动有如下
- https://github.com/garyburd/redigo (推荐) - https://github.com/garyburd/redigo (推荐)
- https://github.com/go-redis/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/
- https://github.com/simonz05/godis - https://github.com/simonz05/godis
- https://github.com/hoisie/redis.go
我以redigo驱动为例来演示如何进行数据的操作: 我以redigo驱动为例来演示如何进行数据的操作:
```Go ```Go
@@ -145,6 +145,12 @@ MongoDB是一个高性能开源无模式的文档型数据库是一个
目前Go支持mongoDB最好的驱动就是[mgo](http://labix.org/mgo)这个驱动目前最有可能成为官方的pkg。 目前Go支持mongoDB最好的驱动就是[mgo](http://labix.org/mgo)这个驱动目前最有可能成为官方的pkg。
安装mgo:
```Go
go get gopkg.in/mgo.v2
```
下面我将演示如何通过Go来操作mongoDB 下面我将演示如何通过Go来操作mongoDB
```Go ```Go
@@ -152,12 +158,13 @@ MongoDB是一个高性能开源无模式的文档型数据库是一个
import ( import (
"fmt" "fmt"
"labix.org/v2/mgo" "gopkg.in/mgo.v2"
"labix.org/v2/mgo/bson" "gopkg.in/mgo.v2/bson"
"log"
) )
type Person struct { type Person struct {
Name string Name string
Phone string Phone string
} }
@@ -168,19 +175,20 @@ MongoDB是一个高性能开源无模式的文档型数据库是一个
} }
defer session.Close() defer session.Close()
// Optional. Switch the session to a monotonic behavior.
session.SetMode(mgo.Monotonic, true) session.SetMode(mgo.Monotonic, true)
c := session.DB("test").C("people") c := session.DB("test").C("people")
err = c.Insert(&Person{"Ale", "+55 53 8116 9639"}, err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
&Person{"Cla", "+55 53 8402 8510"}) &Person{"Cla", "+55 53 8402 8510"})
if err != nil { if err != nil {
panic(err) log.Fatal(err)
} }
result := Person{} result := Person{}
err = c.Find(bson.M{"name": "Ale"}).One(&result) err = c.Find(bson.M{"name": "Ale"}).One(&result)
if err != nil { if err != nil {
panic(err) log.Fatal(err)
} }
fmt.Println("Phone:", result.Phone) fmt.Println("Phone:", result.Phone)
@@ -193,5 +201,5 @@ MongoDB是一个高性能开源无模式的文档型数据库是一个
## links ## links
* [目录](<preface.md>) * [目录](<preface.md>)
* 上一节: [使用beedb库进行ORM开发](<05.5.md>) * 上一节: [使用Beego orm库进行ORM开发](<05.5.md>)
* 下一节: [小结](<05.7.md>) * 下一节: [小结](<05.7.md>)