From 55796f8993ed1bf65f5fbba1821f17e40463e58b Mon Sep 17 00:00:00 2001 From: vCaesar Date: Mon, 2 Jan 2017 17:30:21 +0800 Subject: [PATCH 01/10] Fix Issue #[767] --- zh/08.2.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zh/08.2.md b/zh/08.2.md index 9e5e2236..0573488a 100644 --- a/zh/08.2.md +++ b/zh/08.2.md @@ -24,7 +24,7 @@ WebSocket的协议颇为简单,在第一次handshake通过以后,连接便 图8.3 WebSocket的request和response信息 -在请求中的"Sec-WebSocket-Key"是随机的,对于整天跟编码打交到的程序员,一眼就可以看出来:这个是一个经过base64编码后的数据。服务器端接收到这个请求之后需要把这个字符串连接上一个固定的字符串: +在请求中的"Sec-WebSocket-Key"是随机的,对于整天跟编码打交道的程序员,一眼就可以看出来:这个是一个经过base64编码后的数据。服务器端接收到这个请求之后需要把这个字符串连接上一个固定的字符串: 258EAFA5-E914-47DA-95CA-C5AB0DC85B11 From ad2fc463f0e2be0a105fdf3fad22136ceb54dc06 Mon Sep 17 00:00:00 2001 From: vCaesar Date: Mon, 2 Jan 2017 17:49:29 +0800 Subject: [PATCH 02/10] Update en vim --- en/01.4.md | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/en/01.4.md b/en/01.4.md index 1d21d0a0..5e799dcd 100644 --- a/en/01.4.md +++ b/en/01.4.md @@ -145,6 +145,60 @@ First, download the version of [Sublime](http://www.sublimetext.com/) suitable f Vim is a popular text editor for programmers, which evolved from its slimmer predecessor, Vi. It has functions for intelligent completion, compilation and jumping to errors. +vim-go is vim above an open-source go language using the most extensive development environment plug-ins + +The plugin address:[github.com/fatih/vim-go](https://github.com/fatih/vim-go) + +Vim plugin management are the mainstream [Pathogen](https://github.com/tpope/vim-pathogen) and [Vundle](https://github.com/VundleVim/Vundle.vim) +,But the aspects thereof are different. +Pathogen is to solve each plug-in after the installation of files scattered to multiple directories and poor management of the existence. Vundle is to solve the automatic search and download plug-ins exist. +These two plug-ins can be used simultaneously. + +1.Install Vundle + +```sh +mkdir ~/.vim/bundle +git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim +``` + +Edit .vimrc,Vundle the relevant configuration will be placed in the beginning([Refer to the Vundle documentation for details](https://github.com/VundleVim/Vundle.vim)) + +```sh +set nocompatible " be iMproved, required +filetype off " required + +" set the runtime path to include Vundle and initialize +set rtp+=~/.vim/bundle/Vundle.vim +call vundle#begin() + +" let Vundle manage Vundle, required +Plugin 'gmarik/Vundle.vim' + +" All of your Plugins must be added before the following line +call vundle#end() " required +filetype plugin indent on " required + +``` +2.Install Vim-go + +Edit ~/.vimrc,Add a line between vundle #begin and vundle #end: + +```sh + +Plugin 'fatih/vim-go' +``` + +Executed within Vim: PluginInstall + +3.Install YCM(Your Complete Me) to AutoComplete +Add a line to ~ / .vimrc: +```sh + +Plugin 'Valloric/YouCompleteMe' +``` +Executed within Vim: PluginInstall + + ![](images/1.4.vim.png?raw=true) Figure 1.8 Vim intelligent completion for Go From 49e2826f6ca1c910441b1e18f8be7c6e0aada3fb Mon Sep 17 00:00:00 2001 From: vCaesar Date: Mon, 2 Jan 2017 23:50:48 +0800 Subject: [PATCH 03/10] Fix en/LiteIDE Download page --- en/01.4.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/en/01.4.md b/en/01.4.md index 5e799dcd..55b0c4d2 100644 --- a/en/01.4.md +++ b/en/01.4.md @@ -47,7 +47,7 @@ LiteIDE features. ### LiteIDE installation - Install LiteIDE - - [Download page](http://code.google.com/p/golangide) + - [Download page](https://sourceforge.net/projects/liteide/files/) - [Source code](https://github.com/visualfc/liteide) You need to install Go first, then download the version appropriate for your operating system. Decompress the package to directly use it. From a740c5782ef69f57fe2f632eed42629e6d6a018f Mon Sep 17 00:00:00 2001 From: vCaesar Date: Sat, 7 Jan 2017 18:11:12 +0800 Subject: [PATCH 04/10] Update sublime link and Fix vim link --- zh/01.4.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zh/01.4.md b/zh/01.4.md index 2f9d5c64..f52a6042 100644 --- a/zh/01.4.md +++ b/zh/01.4.md @@ -114,7 +114,7 @@ 接下来就开始讲如何安装,下载[Sublime](http://www.sublimetext.com/) - 根据自己相应的系统下载相应的版本,然后打开Sublime,对于不熟悉Sublime的同学可以先看一下这篇文章[Sublime Text 2 入门及技巧](http://lucifr.com/139225/sublime-text-2-tricks-and-tips/) + 根据自己相应的系统下载相应的版本,然后打开Sublime,对于不熟悉Sublime的同学可以先看一下这篇文章[Sublime Text 全程指南](http://blog.jobbole.com/88648/)或者[sublime text3入门教程](http://blog.csdn.net/sam976/article/details/52076271) 1. 打开之后安装 Package Control:Ctrl+` 打开命令行,执行如下代码: @@ -406,7 +406,7 @@ Plugin 'Valloric/YouCompleteMe' 5. 恭喜你,安装完成,你现在可以使用`:e main.go`体验一下开发Go的乐趣。 -更多VIM 设定, 可参考[链接](http://monnand.me/p/vim-golang-environment/zhCN/) +更多VIM 设定, 可参考[链接](http://www.cnblogs.com/witcxc/archive/2011/12/28/2304704.html) ## Emacs Emacs传说中的神器,她不仅仅是一个编辑器,它是一个整合环境,或可称它为集成开发环境,这些功能如让使用者置身于全功能的操作系统中。 From 1c386c6a54d1786a8aae7e052c394ac7250a3bc1 Mon Sep 17 00:00:00 2001 From: vCaesar Date: Sat, 7 Jan 2017 18:42:19 +0800 Subject: [PATCH 05/10] Add redigo demo --- zh/05.6.md | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/zh/05.6.md b/zh/05.6.md index 6b8ef977..8f1434d9 100644 --- a/zh/05.6.md +++ b/zh/05.6.md @@ -15,7 +15,85 @@ Go目前支持redis的驱动有如下 - https://github.com/simonz05/godis - https://github.com/hoisie/redis.go -目前我fork了最后一个驱动,更新了一些bug,目前应用在我自己的短域名服务项目中(每天200W左右的PV值) +我以redigo驱动为例来演示如何进行数据的操作: +```Go + + package redis + + import ( + "fmt" + "github.com/garyburd/redigo/redis" + "os" + "os/signal" + "syscall" + "time" + ) + + var ( + Pool *redis.Pool + ) + + func init() { + redisHost := ":6379" + Pool = newPool(redisHost) + close() + } + + func newPool(server string) *redis.Pool { + + return &redis.Pool{ + + MaxIdle: 3, + IdleTimeout: 240 * time.Second, + + Dial: func() (redis.Conn, error) { + c, err := redis.Dial("tcp", server) + if err != nil { + return nil, err + } + return c, err + }, + + TestOnBorrow: func(c redis.Conn, t time.Time) error { + _, err := c.Do("PING") + return err + }, + } + } + + func close() { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + signal.Notify(c, syscall.SIGTERM) + signal.Notify(c, syscall.SIGKILL) + go func() { + <-c + Pool.Close() + os.Exit(0) + }() + } + + func Get(key string) ([]byte, error) { + + conn := Pool.Get() + defer conn.Close() + + var data []byte + data, err := redis.Bytes(conn.Do("GET", key)) + if err != nil { + return data, fmt.Errorf("error get key %s: %v", key, err) + } + return data, err + } + + func main() { + test, err := Get("test") + fmt.Println(test, err) + } + +``` + +另外以前我fork了最后一个驱动,更新了一些bug,目前应用在我自己的短域名服务项目中(每天200W左右的PV值) https://github.com/astaxie/goredis From 2f732d86bd4543bc8793a0a1fe6fc7fd8d7ee144 Mon Sep 17 00:00:00 2001 From: vCaesar Date: Sat, 7 Jan 2017 19:04:20 +0800 Subject: [PATCH 06/10] Add en/redigo demo --- en/05.6.md | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ zh/05.6.md | 2 +- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/en/05.6.md b/en/05.6.md index 08b0d2c7..05f023ce 100644 --- a/en/05.6.md +++ b/en/05.6.md @@ -15,6 +15,84 @@ There are some Go database drivers for redis: - [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 + + package main + + import ( + "fmt" + "github.com/garyburd/redigo/redis" + "os" + "os/signal" + "syscall" + "time" + ) + + var ( + Pool *redis.Pool + ) + + func init() { + redisHost := ":6379" + Pool = newPool(redisHost) + close() + } + + func newPool(server string) *redis.Pool { + + return &redis.Pool{ + + MaxIdle: 3, + IdleTimeout: 240 * time.Second, + + Dial: func() (redis.Conn, error) { + c, err := redis.Dial("tcp", server) + if err != nil { + return nil, err + } + return c, err + }, + + TestOnBorrow: func(c redis.Conn, t time.Time) error { + _, err := c.Do("PING") + return err + }, + } + } + + func close() { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + signal.Notify(c, syscall.SIGTERM) + signal.Notify(c, syscall.SIGKILL) + go func() { + <-c + Pool.Close() + os.Exit(0) + }() + } + + func Get(key string) ([]byte, error) { + + conn := Pool.Get() + defer conn.Close() + + var data []byte + data, err := redis.Bytes(conn.Do("GET", key)) + if err != nil { + return data, fmt.Errorf("error get key %s: %v", key, err) + } + return data, err + } + + func main() { + test, err := Get("test") + fmt.Println(test, err) + } + +``` + I forked the last of these packages, fixed some bugs, and used it in my short URL service (2 million PV every day). - [https://github.com/astaxie/goredis](https://github.com/astaxie/goredis) diff --git a/zh/05.6.md b/zh/05.6.md index 8f1434d9..9b43866c 100644 --- a/zh/05.6.md +++ b/zh/05.6.md @@ -18,7 +18,7 @@ Go目前支持redis的驱动有如下 我以redigo驱动为例来演示如何进行数据的操作: ```Go - package redis + package main import ( "fmt" From 13215d259c30e79d32bdf08e979a0f4c16ef6879 Mon Sep 17 00:00:00 2001 From: vCaesar Date: Sun, 8 Jan 2017 20:37:28 +0800 Subject: [PATCH 07/10] Update preface word --- zh/01.0.md | 2 +- zh/01.2.md | 2 +- zh/05.4.md | 2 +- zh/05.6.md | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/zh/01.0.md b/zh/01.0.md index 4aa68fb1..09fb00ac 100644 --- a/zh/01.0.md +++ b/zh/01.0.md @@ -20,4 +20,4 @@ Go是一种编译型语言,它结合了解释型语言的游刃有余,动态 ## links * [目录]() - * 下一节: [Go安装](<01.1.md>) + * 下一节: [安装Go](<01.1.md>) diff --git a/zh/01.2.md b/zh/01.2.md index a1bdf280..5c708666 100644 --- a/zh/01.2.md +++ b/zh/01.2.md @@ -176,5 +176,5 @@ go get本质上可以理解为首先第一步是通过源码工具clone代码到 ## links * [目录]() - * 上一节: [GO安装](<01.1.md>) + * 上一节: [安装Go](<01.1.md>) * 下一节: [GO 命令](<01.3.md>) diff --git a/zh/05.4.md b/zh/05.4.md index 7b75671a..da81ec06 100644 --- a/zh/05.4.md +++ b/zh/05.4.md @@ -131,4 +131,4 @@ Go实现的支持PostgreSQL的驱动也很多,因为国外很多人在开发 ## links * [目录]() * 上一节: [使用SQLite数据库](<05.3.md>) - * 下一节: [使用beedb库进行ORM开发](<05.5.md>) + * 下一节: [使用Beego orm库进行ORM开发](<05.5.md>) diff --git a/zh/05.6.md b/zh/05.6.md index 9b43866c..e72c5f02 100644 --- a/zh/05.6.md +++ b/zh/05.6.md @@ -193,5 +193,5 @@ MongoDB是一个高性能,开源,无模式的文档型数据库,是一个 ## links * [目录]() - * 上一节: [使用beedb库进行ORM开发](<05.5.md>) + * 上一节: [使用Beego orm库进行ORM开发](<05.5.md>) * 下一节: [小结](<05.7.md>) From 4deec3503558e9d58232c32af0662e546c682e4f Mon Sep 17 00:00:00 2001 From: vCaesar Date: Sun, 8 Jan 2017 21:06:51 +0800 Subject: [PATCH 08/10] Update redis link and Update mgo --- en/05.6.md | 75 +++++++++++++++++++++++++++++++----------------------- zh/05.6.md | 56 +++++++++++++++++++++++----------------- 2 files changed, 75 insertions(+), 56 deletions(-) diff --git a/en/05.6.md b/en/05.6.md index 05f023ce..0fa888c6 100644 --- a/en/05.6.md +++ b/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 diff --git a/zh/05.6.md b/zh/05.6.md index e72c5f02..f42032f9 100644 --- a/zh/05.6.md +++ b/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) } ``` From 52fcfce8e4fe0b604dfde192691e71ef2af94766 Mon Sep 17 00:00:00 2001 From: vCaesar Date: Sun, 8 Jan 2017 21:22:13 +0800 Subject: [PATCH 09/10] Update other mgo --- de/05.6.md | 69 +++++++++++++++++++++++++++------------------------ en/05.6.md | 44 ++++++++++++++++---------------- es/05.6.md | 52 ++++++++++++++++++++------------------ ja/05.6.md | 16 ++++++++---- pt-br/05.6.md | 66 ++++++++++++++++++++++++++---------------------- zh/05.6.md | 44 ++++++++++++++++---------------- 6 files changed, 156 insertions(+), 135 deletions(-) diff --git a/de/05.6.md b/de/05.6.md index bf3bdc0a..930dd1e8 100644 --- a/de/05.6.md +++ b/de/05.6.md @@ -66,44 +66,49 @@ The best driver for mongoDB is called `mgo`, and it is possible that it will be Here is the example: +```Go + package main - + import ( - "fmt" - "labix.org/v2/mgo" - "labix.org/v2/mgo/bson" + "fmt" + "gopkg.in/mgo.v2" + "gopkg.in/mgo.v2/bson" + "log" ) - + type Person struct { - 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) + Name string + Phone string } + 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. ## Links diff --git a/en/05.6.md b/en/05.6.md index 0fa888c6..4f83df2c 100644 --- a/en/05.6.md +++ b/en/05.6.md @@ -155,40 +155,40 @@ Here is the example: import ( "fmt" - "log" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" + "log" ) 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() - // Optional. Switch the session to a monotonic behavior. - 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 { - log.Fatal(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 { - 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) + fmt.Println("Phone:", result.Phone) } ``` diff --git a/es/05.6.md b/es/05.6.md index b69f6da8..3a1096ba 100644 --- a/es/05.6.md +++ b/es/05.6.md @@ -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. Aquí está un ejemplo -``` +```Go + package main import ( - "fmt" - "labix.org/v2/mgo" - "labix.org/v2/mgo/bson" + "fmt" + "gopkg.in/mgo.v2" + "gopkg.in/mgo.v2/bson" + "log" ) 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) } + ``` 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. diff --git a/ja/05.6.md b/ja/05.6.md index 36c80693..e59e6f4a 100644 --- a/ja/05.6.md +++ b/ja/05.6.md @@ -66,16 +66,19 @@ MongoDBは高性能でオープンソース、モードレスなドキュメン 次にどのようにしてGoからmongoDBを操作するのかご説明します: +```Go + package main import ( "fmt" - "labix.org/v2/mgo" - "labix.org/v2/mgo/bson" + "gopkg.in/mgo.v2" + "gopkg.in/mgo.v2/bson" + "log" ) type Person struct { - Name string + Name string Phone string } @@ -86,24 +89,27 @@ MongoDBは高性能でオープンソース、モードレスなドキュメン } 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 { - panic(err) + log.Fatal(err) } result := Person{} err = c.Find(bson.M{"name": "Ale"}).One(&result) if err != nil { - panic(err) + log.Fatal(err) } fmt.Println("Phone:", result.Phone) } +``` + mgoの操作方法とbeedbの操作方法はほとんど似ていることがわかります。どちらもstructに基づいて操作する方法です。これこそがGo Styleです。 diff --git a/pt-br/05.6.md b/pt-br/05.6.md index bf3bdc0a..ab798a70 100644 --- a/pt-br/05.6.md +++ b/pt-br/05.6.md @@ -66,44 +66,50 @@ The best driver for mongoDB is called `mgo`, and it is possible that it will be Here is the example: +```Go + package main - + import ( - "fmt" - "labix.org/v2/mgo" - "labix.org/v2/mgo/bson" + "fmt" + "gopkg.in/mgo.v2" + "gopkg.in/mgo.v2/bson" + "log" ) - + 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 diff --git a/zh/05.6.md b/zh/05.6.md index f42032f9..fd913fb6 100644 --- a/zh/05.6.md +++ b/zh/05.6.md @@ -158,40 +158,40 @@ MongoDB是一个高性能,开源,无模式的文档型数据库,是一个 import ( "fmt" - "log" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" + "log" ) 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() - // Optional. Switch the session to a monotonic behavior. - 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 { - log.Fatal(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 { - 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) + fmt.Println("Phone:", result.Phone) } ``` From 5a9bedb1e9424c72669112711c763db0932e724c Mon Sep 17 00:00:00 2001 From: Michael van Tricht Date: Sun, 8 Jan 2017 22:24:02 +0100 Subject: [PATCH 10/10] Fix small error in 02.3.md --- en/02.3.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/en/02.3.md b/en/02.3.md index 3c8e3bcc..5fd21659 100644 --- a/en/02.3.md +++ b/en/02.3.md @@ -507,7 +507,7 @@ There are some special operators when we import packages, and beginners are alwa _ "github.com/ziutek/mymysql/godrv" ) - The `_` operator actually means we just want to import that package and execute its `init` function, and we are not sure if want to use the functions belonging to that package. + The `_` operator actually means we just want to import that package and execute its `init` function, and we are not sure if we want to use the functions belonging to that package. ## Links