Merge remote-tracking branch 'upstream/master'

This commit is contained in:
ma0
2017-01-09 22:44:21 -05:00
13 changed files with 390 additions and 140 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:
```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

View File

@@ -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.
@@ -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 .vimrcVundle 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 ~/.vimrcAdd 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

View File

@@ -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

View File

@@ -10,10 +10,88 @@ 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
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).
@@ -64,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"
"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

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

View File

@@ -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です。

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:
```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

View File

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

View File

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

View File

@@ -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 ControlCtrl+` 打开命令行,执行如下代码:
@@ -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传说中的神器她不仅仅是一个编辑器它是一个整合环境或可称它为集成开发环境这些功能如让使用者置身于全功能的操作系统中。

View File

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

View File

@@ -10,12 +10,90 @@ 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
目前我fork了最后一个驱动更新了一些bug目前应用在我自己的短域名服务项目中(每天200W左右的PV值)
我以redigo驱动为例来演示如何进行数据的操作:
```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)
}
```
另外以前我fork了最后一个驱动更新了一些bug目前应用在我自己的短域名服务项目中(每天200W左右的PV值)
https://github.com/astaxie/goredis
@@ -67,6 +145,12 @@ MongoDB是一个高性能开源无模式的文档型数据库是一个
目前Go支持mongoDB最好的驱动就是[mgo](http://labix.org/mgo)这个驱动目前最有可能成为官方的pkg。
安装mgo:
```Go
go get gopkg.in/mgo.v2
```
下面我将演示如何通过Go来操作mongoDB
```Go
@@ -74,12 +158,13 @@ MongoDB是一个高性能开源无模式的文档型数据库是一个
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
}
@@ -90,19 +175,20 @@ 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)
@@ -115,5 +201,5 @@ MongoDB是一个高性能开源无模式的文档型数据库是一个
## links
* [目录](<preface.md>)
* 上一节: [使用beedb库进行ORM开发](<05.5.md>)
* 上一节: [使用Beego orm库进行ORM开发](<05.5.md>)
* 下一节: [小结](<05.7.md>)

View File

@@ -24,7 +24,7 @@ WebSocket的协议颇为简单在第一次handshake通过以后连接便
图8.3 WebSocket的request和response信息
在请求中的"Sec-WebSocket-Key"是随机的,对于整天跟编码打交的程序员一眼就可以看出来这个是一个经过base64编码后的数据。服务器端接收到这个请求之后需要把这个字符串连接上一个固定的字符串
在请求中的"Sec-WebSocket-Key"是随机的,对于整天跟编码打交的程序员一眼就可以看出来这个是一个经过base64编码后的数据。服务器端接收到这个请求之后需要把这个字符串连接上一个固定的字符串
258EAFA5-E914-47DA-95CA-C5AB0DC85B11