1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
*~
|
||||
pkg/*
|
||||
*.html
|
||||
*.exe
|
||||
|
||||
|
||||
13
README.md
13
README.md
@@ -1,13 +1,12 @@
|
||||
# 《Go Web 编程》
|
||||
因为自己对Web开发比较感兴趣,所以最近抽空在写一本开源的书籍《Go Web编程》《Build Web Application with Golang》。写这本书不表示我能力很强,而是我愿意分享,和大家一起分享Go写Web应用的一些东西。
|
||||
目前这本书已经出版,如果你觉得内容还可以,你可以通过下面几个途径购买,谢谢支持:
|
||||
|
||||
- 对于从PHP/Python/Ruby转过来的同学了解Go怎么写Web应用开发的
|
||||
- [chinapub](http://product.china-pub.com/3767290)
|
||||
- [当当网](http://product.dangdang.com/product.aspx?product_id=23231404)
|
||||
- [京东](http://book.jd.com/11224644.html)
|
||||
- [Amazon](http://www.amazon.cn/Go-Web%E7%BC%96%E7%A8%8B-%E8%B0%A2%E5%AD%9F%E5%86%9B/dp/B00CHWVAHQ/ref=sr_1_1?s=books&ie=UTF8&qid=1369323453&sr=1-1)
|
||||
|
||||
- 对于从C/C++转过来的同学了解Web到底是怎么运行起来的
|
||||
|
||||
我一直认为知识是用来分享的,让更多的人分享自己拥有的一切知识这个才是人生最大的快乐。
|
||||
|
||||
这本书目前我放在Github上,我现在基本每天晚上抽空会写一些,时间有限、能力有限,所以希望更多的朋友参与到这个开源项目中来。
|
||||

|
||||
|
||||
# 通过捐款支持本书
|
||||
如果你喜欢这本《Go Web编程》的话, 可以通过捐款的方式, 支持作者继续更新本书或者做出其他更多好玩好用的开源应用: 比如为本书修补漏洞、添加更多有趣的章节, 或者发行有更多更棒内容的下一版,或者改善beego等等。
|
||||
|
||||
@@ -114,22 +114,29 @@ gvm是第三方开发的Go多版本管理工具,类似ruby里面的rvm工具
|
||||
|
||||
安装完成后我们就可以安装go了:
|
||||
|
||||
gvm install go1.0.3
|
||||
gvm use go1.0.3
|
||||
gvm install go1.1
|
||||
gvm use go1.1
|
||||
|
||||
也可以使用下面的命令,省去每次调用gvm use的麻烦:
|
||||
gvm use go1.1 --default
|
||||
|
||||
执行完上面的命令之后GOPATH、GOROOT等环境变量会自动设置好,这样就可以直接使用了。
|
||||
|
||||
### apt-get
|
||||
Ubuntu是目前使用最多的Linux桌面系统,使用`apt-get`命令来管理软件包,我们可以通过下面的命令来安装Go:
|
||||
Ubuntu是目前使用最多的Linux桌面系统,使用`apt-get`命令来管理软件包,我们可以通过下面的命令来安装Go,为了以后方便,应该把 `git` `mercurial` 也安装上:
|
||||
|
||||
sudo apt-get install python-software-properties
|
||||
sudo add-apt-repository ppa:gophers/go
|
||||
sudo apt-get update
|
||||
sudo apt-get install golang-stable
|
||||
sudo apt-get install golang-stable git-core mercurial
|
||||
|
||||
### homebrew
|
||||
homebrew是Mac系统下面目前使用最多的管理软件的工具,目前已支持Go,可以通过命令直接安装Go:
|
||||
homebrew是Mac系统下面目前使用最多的管理软件的工具,目前已支持Go,可以通过命令直接安装Go,为了以后方便,应该把 `git` `mercurial` 也安装上:
|
||||
|
||||
brew update && brew upgrade
|
||||
brew install go
|
||||
brew install git
|
||||
brew install mercurial
|
||||
|
||||
|
||||
## links
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
```sh
|
||||
export GOPATH=/home/apple/mygo
|
||||
```
|
||||
为了方便,应该把新建以上文件夹,并且把以上一行加入到 `.bashrc` 或者 `.zshrc` 或者自己的 `sh` 的配置文件中。
|
||||
|
||||
Windows 设置如下,新建一个环境变量名称叫做GOPATH:
|
||||
```sh
|
||||
GOPATH=c:\mygo
|
||||
|
||||
@@ -149,6 +149,14 @@
|
||||
|
||||
如果没有出现这样的提示,一般就是你的`$PATH`没有配置正确。你可以打开终端,输入gocode,是不是能够正确运行,如果不行就说明`$PATH`没有配置正确。
|
||||
|
||||
4. MacOS下已经设置了$GOROOT, $GOPATH, $GOBIN,还是没有自动提示怎么办。
|
||||
|
||||
请在sublime中使用command + 9, 然后输入env检查$PATH, GOROOT, $GOPATH, $GOBIN等变量, 如果没有请采用下面的方法。
|
||||
|
||||
首先建立下面的连接, 然后从Terminal中直接启动sublime
|
||||
|
||||
ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime
|
||||
|
||||
|
||||
## Vim
|
||||
Vim是从vi发展出来的一个文本编辑器, 代码补全、编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用。
|
||||
|
||||
@@ -276,7 +276,7 @@ method的语法如下:
|
||||
}
|
||||
|
||||
### method重写
|
||||
上面的例子中,如果Emplyee想要实现自己的SayHi,怎么办?简单,和匿名字段冲突一样的道理,我们可以在Emplyee上面定义一个method,重写了匿名字段的方法。请看下面的例子
|
||||
上面的例子中,如果Employee想要实现自己的SayHi,怎么办?简单,和匿名字段冲突一样的道理,我们可以在Employee上面定义一个method,重写了匿名字段的方法。请看下面的例子
|
||||
|
||||
package main
|
||||
import "fmt"
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<title></title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="http://127.0.0.1:9090/login" method="post">
|
||||
<form action="/login" method="post">
|
||||
用户名:<input type="text" name="username">
|
||||
密码:<input type="password" name="password">
|
||||
<input type="submit" value="登陆">
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
## MySQL驱动
|
||||
Go中支持MySQL的驱动目前比较多,有如下几种,有些是支持database/sql标准,而有些是采用了自己的实现接口,常用的有如下几种:
|
||||
|
||||
- https://github.com/Go-SQL-Driver/MySQL 支持database/sql,全部采用go写。
|
||||
- https://github.com/go-sql-driver/mysql 支持database/sql,全部采用go写。
|
||||
- https://github.com/ziutek/mymysql 支持database/sql,也支持自定义的接口,全部采用go写。
|
||||
- https://github.com/Philio/GoMySQL 不支持database/sql,自定义接口,全部采用go写。
|
||||
|
||||
@@ -114,7 +114,7 @@ Go中支持MySQL的驱动目前比较多,有如下几种,有些是支持data
|
||||
|
||||
关键的几个函数我解释一下:
|
||||
|
||||
sql.Open()函数用来打开一个注册过的数据库驱动,Go-MySQL-Driver中注册了mysql这个数据库驱动,第二个参数是DNS(Data Source Name),它是Go-MySQL-Driver定义的一些数据库链接和配置信息。它支持如下格式:
|
||||
sql.Open()函数用来打开一个注册过的数据库驱动,Go-MySQL-Driver中注册了mysql这个数据库驱动,第二个参数是DSN(Data Source Name),它是Go-MySQL-Driver定义的一些数据库链接和配置信息。它支持如下格式:
|
||||
|
||||
user@unix(/path/to/socket)/dbname?charset=utf8
|
||||
user:password@tcp(localhost:5555)/dbname?charset=utf8
|
||||
|
||||
@@ -81,19 +81,19 @@ Go实现整个的流程应该也是这样的,在main包中创建一个全局
|
||||
|
||||
>以上设计思路来源于database/sql/driver,先定义好接口,然后具体的存储session的结构实现相应的接口并注册后,相应功能这样就可以使用了,以下是用来随需注册存储session的结构的Register函数的实现。
|
||||
|
||||
var provides = make(map[string]Provide)
|
||||
var provides = make(map[string]Provider)
|
||||
|
||||
// Register makes a session provide available by the provided name.
|
||||
// If Register is called twice with the same name or if driver is nil,
|
||||
// it panics.
|
||||
func Register(name string, provide Provide) {
|
||||
if driver == nil {
|
||||
func Register(name string, provider Provider) {
|
||||
if provider == nil {
|
||||
panic("session: Register provide is nil")
|
||||
}
|
||||
if _, dup := provides[name]; dup {
|
||||
panic("session: Register called twice for provide " + name)
|
||||
}
|
||||
provides[name] = provide
|
||||
provides[name] = provider
|
||||
}
|
||||
|
||||
### 全局唯一的Session ID
|
||||
|
||||
@@ -88,6 +88,8 @@ JSON(Javascript Object Notation)是一种轻量级的数据交换语言,
|
||||
fmt.Println(k, "is string", vv)
|
||||
case int:
|
||||
fmt.Println(k, "is int", vv)
|
||||
case float64:
|
||||
fmt.Println(k,"is float64",vv)
|
||||
case []interface{}:
|
||||
fmt.Println(k, "is an array:")
|
||||
for i, u := range vv {
|
||||
|
||||
@@ -126,7 +126,6 @@ DeleteController
|
||||
}
|
||||
|
||||
func (this *DeleteController) Get() {
|
||||
inputs := this.Input()
|
||||
id, _ := strconv.Atoi(this.Ctx.Params[":id"])
|
||||
this.Data["Post"] = models.DelBlog(id)
|
||||
this.Ctx.Redirect(302, "/")
|
||||
@@ -167,7 +166,7 @@ DeleteController
|
||||
|
||||
func GetBlog(id int) (blog Blog) {
|
||||
db := GetLink()
|
||||
db.Where("id=?", id).Find(&blogs)
|
||||
db.Where("id=?", id).Find(&blog)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -256,4 +255,4 @@ edit.tpl
|
||||
## links
|
||||
* [目录](<preface.md>)
|
||||
* 上一章: [数据库操作](<13.4.md>)
|
||||
* 下一节: [小结](<13.6.md>)
|
||||
* 下一节: [小结](<13.6.md>)
|
||||
|
||||
@@ -2,7 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/russross/blackfriday"
|
||||
"github.com/fairlyblank/md2min"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
// 定义一个访问者结构体
|
||||
type Visitor struct{}
|
||||
type Visitor struct {}
|
||||
|
||||
func (self *Visitor) visit(path string, f os.FileInfo, err error) error {
|
||||
if f == nil {
|
||||
@@ -30,21 +30,18 @@ func (self *Visitor) visit(path string, f os.FileInfo, err error) error {
|
||||
}
|
||||
input, _ := ioutil.ReadAll(file)
|
||||
input = regexp.MustCompile("\\[(.*?)\\]\\(<?(.*?)\\.md>?\\)").ReplaceAll(input, []byte("[$1](<$2.html>)"))
|
||||
output := blackfriday.MarkdownCommon(input)
|
||||
var out *os.File
|
||||
if out, err = os.Create(strings.Replace(f.Name(), ".md", ".html", -1)); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error creating %s: %v", f.Name(), err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
defer out.Close()
|
||||
header := "<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\"/>\n</head>\n"
|
||||
footer := "</html>\n"
|
||||
out.Write([]byte(header))
|
||||
if _, err = out.Write(output); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Error writing output:", err)
|
||||
md := md2min.New("none")
|
||||
err = md.Parse(input, out)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Parsing Error", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
out.Write([]byte(footer))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
BIN
ebook/images/ebook.jpg
Normal file
BIN
ebook/images/ebook.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 50 KiB |
Reference in New Issue
Block a user