Skip to content

Commit 7d3c3c7

Browse files
author
ma0
committed
Merge remote-tracking branch 'upstream/master'
2 parents 852ec80 + 2aa96af commit 7d3c3c7

File tree

13 files changed

+392
-142
lines changed

13 files changed

+392
-142
lines changed

de/05.6.md

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -66,44 +66,49 @@ The best driver for mongoDB is called `mgo`, and it is possible that it will be
6666

6767
Here is the example:
6868

69+
```Go
70+
6971
package main
70-
72+
7173
import (
72-
"fmt"
73-
"labix.org/v2/mgo"
74-
"labix.org/v2/mgo/bson"
74+
"fmt"
75+
"gopkg.in/mgo.v2"
76+
"gopkg.in/mgo.v2/bson"
77+
"log"
7578
)
76-
79+
7780
type Person struct {
78-
Name string
79-
Phone string
81+
Name string
82+
Phone string
8083
}
81-
84+
8285
func main() {
83-
session, err := mgo.Dial("server1.example.com,server2.example.com")
84-
if err != nil {
85-
panic(err)
86-
}
87-
defer session.Close()
88-
89-
session.SetMode(mgo.Monotonic, true)
90-
91-
c := session.DB("test").C("people")
92-
err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
93-
&Person{"Cla", "+55 53 8402 8510"})
94-
if err != nil {
95-
panic(err)
96-
}
97-
98-
result := Person{}
99-
err = c.Find(bson.M{"name": "Ale"}).One(&result)
100-
if err != nil {
101-
panic(err)
102-
}
103-
104-
fmt.Println("Phone:", result.Phone)
86+
session, err := mgo.Dial("server1.example.com,server2.example.com")
87+
if err != nil {
88+
panic(err)
89+
}
90+
defer session.Close()
91+
92+
// Optional. Switch the session to a monotonic behavior.
93+
session.SetMode(mgo.Monotonic, true)
94+
95+
c := session.DB("test").C("people")
96+
err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
97+
&Person{"Cla", "+55 53 8402 8510"})
98+
if err != nil {
99+
log.Fatal(err)
100+
}
101+
102+
result := Person{}
103+
err = c.Find(bson.M{"name": "Ale"}).One(&result)
104+
if err != nil {
105+
log.Fatal(err)
106+
}
107+
108+
fmt.Println("Phone:", result.Phone)
105109
}
106110

111+
```
107112
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.
108113

109114
## Links

en/01.4.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ LiteIDE features.
4747
### LiteIDE installation
4848

4949
- Install LiteIDE
50-
- [Download page](http://code.google.com/p/golangide)
50+
- [Download page](https://sourceforge.net/projects/liteide/files/)
5151
- [Source code](https://github.com/visualfc/liteide)
5252

5353
You need to install Go first, then download the version appropriate for your operating system. Decompress the package to directly use it.
@@ -145,6 +145,60 @@ First, download the version of [Sublime](http://www.sublimetext.com/) suitable f
145145

146146
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.
147147

148+
vim-go is vim above an open-source go language using the most extensive development environment plug-ins
149+
150+
The plugin address:[github.com/fatih/vim-go](https://github.com/fatih/vim-go)
151+
152+
Vim plugin management are the mainstream [Pathogen](https://github.com/tpope/vim-pathogen) and [Vundle](https://github.com/VundleVim/Vundle.vim)
153+
,But the aspects thereof are different.
154+
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.
155+
These two plug-ins can be used simultaneously.
156+
157+
1.Install Vundle
158+
159+
```sh
160+
mkdir ~/.vim/bundle
161+
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
162+
```
163+
164+
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))
165+
166+
```sh
167+
set nocompatible " be iMproved, required
168+
filetype off " required
169+
170+
" set the runtime path to include Vundle and initialize
171+
set rtp+=~/.vim/bundle/Vundle.vim
172+
call vundle#begin()
173+
174+
" let Vundle manage Vundle, required
175+
Plugin 'gmarik/Vundle.vim'
176+
177+
" All of your Plugins must be added before the following line
178+
call vundle#end() " required
179+
filetype plugin indent on " required
180+
181+
```
182+
2.Install Vim-go
183+
184+
Edit ~/.vimrc,Add a line between vundle #begin and vundle #end:
185+
186+
```sh
187+
188+
Plugin 'fatih/vim-go'
189+
```
190+
191+
Executed within Vim: PluginInstall
192+
193+
3.Install YCM(Your Complete Me) to AutoComplete
194+
Add a line to ~ / .vimrc:
195+
```sh
196+
197+
Plugin 'Valloric/YouCompleteMe'
198+
```
199+
Executed within Vim: PluginInstall
200+
201+
148202
![](images/1.4.vim.png?raw=true)
149203
150204
Figure 1.8 Vim intelligent completion for Go

en/02.3.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ There are some special operators when we import packages, and beginners are alwa
507507
_ "github.com/ziutek/mymysql/godrv"
508508
)
509509

510-
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.
510+
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.
511511

512512
## Links
513513

en/05.6.md

Lines changed: 121 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,88 @@ redis is a key-value storage system like Memcached, that supports the string, li
1010

1111
There are some Go database drivers for redis:
1212
- [https://github.com/garyburd/redigo](https://github.com/garyburd/redigo)
13+
- [https://github.com/go-redis/redis](https://github.com/go-redis/redis)
14+
- [https://github.com/hoisie/redis](https://github.com/hoisie/redis)
1315
- [https://github.com/alphazero/Go-Redis](https://github.com/alphazero/Go-Redis)
14-
- [http://code.google.com/p/tideland-rdc/](http://code.google.com/p/tideland-rdc/)
1516
- [https://github.com/simonz05/godis](https://github.com/simonz05/godis)
16-
- [https://github.com/hoisie/redis.go](https://github.com/hoisie/redis.go)
17+
18+
Let's see how to use the driver that redigo to operate on a database:
19+
```Go
20+
21+
package main
22+
23+
import (
24+
"fmt"
25+
"github.com/garyburd/redigo/redis"
26+
"os"
27+
"os/signal"
28+
"syscall"
29+
"time"
30+
)
31+
32+
var (
33+
Pool *redis.Pool
34+
)
35+
36+
func init() {
37+
redisHost := ":6379"
38+
Pool = newPool(redisHost)
39+
close()
40+
}
41+
42+
func newPool(server string) *redis.Pool {
43+
44+
return &redis.Pool{
45+
46+
MaxIdle: 3,
47+
IdleTimeout: 240 * time.Second,
48+
49+
Dial: func() (redis.Conn, error) {
50+
c, err := redis.Dial("tcp", server)
51+
if err != nil {
52+
return nil, err
53+
}
54+
return c, err
55+
},
56+
57+
TestOnBorrow: func(c redis.Conn, t time.Time) error {
58+
_, err := c.Do("PING")
59+
return err
60+
},
61+
}
62+
}
63+
64+
func close() {
65+
c := make(chan os.Signal, 1)
66+
signal.Notify(c, os.Interrupt)
67+
signal.Notify(c, syscall.SIGTERM)
68+
signal.Notify(c, syscall.SIGKILL)
69+
go func() {
70+
<-c
71+
Pool.Close()
72+
os.Exit(0)
73+
}()
74+
}
75+
76+
func Get(key string) ([]byte, error) {
77+
78+
conn := Pool.Get()
79+
defer conn.Close()
80+
81+
var data []byte
82+
data, err := redis.Bytes(conn.Do("GET", key))
83+
if err != nil {
84+
return data, fmt.Errorf("error get key %s: %v", key, err)
85+
}
86+
return data, err
87+
}
88+
89+
func main() {
90+
test, err := Get("test")
91+
fmt.Println(test, err)
92+
}
93+
94+
```
1795

1896
I forked the last of these packages, fixed some bugs, and used it in my short URL service (2 million PV every day).
1997

@@ -64,46 +142,57 @@ Figure 5.1 MongoDB compared to Mysql
64142

65143
The best driver for mongoDB is called `mgo`, and it is possible that it will be included in the standard library in the future.
66144

145+
Install mgo:
146+
147+
```Go
148+
go get gopkg.in/mgo.v2
149+
```
150+
67151
Here is the example:
152+
```Go
68153

69154
package main
70-
155+
71156
import (
72-
"fmt"
73-
"labix.org/v2/mgo"
74-
"labix.org/v2/mgo/bson"
157+
"fmt"
158+
"gopkg.in/mgo.v2"
159+
"gopkg.in/mgo.v2/bson"
160+
"log"
75161
)
76-
162+
77163
type Person struct {
78-
Name string
79-
Phone string
164+
Name string
165+
Phone string
80166
}
81-
167+
82168
func main() {
83-
session, err := mgo.Dial("server1.example.com,server2.example.com")
84-
if err != nil {
85-
panic(err)
86-
}
87-
defer session.Close()
88-
89-
session.SetMode(mgo.Monotonic, true)
90-
91-
c := session.DB("test").C("people")
92-
err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
93-
&Person{"Cla", "+55 53 8402 8510"})
94-
if err != nil {
95-
panic(err)
96-
}
97-
98-
result := Person{}
99-
err = c.Find(bson.M{"name": "Ale"}).One(&result)
100-
if err != nil {
101-
panic(err)
102-
}
103-
104-
fmt.Println("Phone:", result.Phone)
169+
session, err := mgo.Dial("server1.example.com,server2.example.com")
170+
if err != nil {
171+
panic(err)
172+
}
173+
defer session.Close()
174+
175+
// Optional. Switch the session to a monotonic behavior.
176+
session.SetMode(mgo.Monotonic, true)
177+
178+
c := session.DB("test").C("people")
179+
err = c.Insert(&Person{"Ale", "+55 53 8116 9639"},
180+
&Person{"Cla", "+55 53 8402 8510"})
181+
if err != nil {
182+
log.Fatal(err)
183+
}
184+
185+
result := Person{}
186+
err = c.Find(bson.M{"name": "Ale"}).One(&result)
187+
if err != nil {
188+
log.Fatal(err)
189+
}
190+
191+
fmt.Println("Phone:", result.Phone)
105192
}
106193

194+
```
195+
107196
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.
108197

109198
## Links

0 commit comments

Comments
 (0)