diff --git a/README_gc.md b/README_gc.md index e953eba..3a13094 100644 --- a/README_gc.md +++ b/README_gc.md @@ -19,4 +19,4 @@ Golang 编程:245386165 |更新日期 |更新内容 |----------|------------------ -|2015-08-08|9.9 通过 Git 打包和安装 \ No newline at end of file +|2015-08-11|9.11 在 Go 程序中使用外部库 \ No newline at end of file diff --git a/TOC.md b/TOC.md index a90c003..d1acef8 100644 --- a/TOC.md +++ b/TOC.md @@ -76,4 +76,6 @@ - 9.6 [为自定义包使用 godoc](eBook/09.6.md) - 9.7 [使用 go install 安装自定义包](eBook/09.7.md) - 9.8 [自定义包的目录结构、go install 和 go test](eBook/09.8.md) - - 9.9 [通过 Git 打包和安装](eBook/09.9.md) \ No newline at end of file + - 9.9 [通过 Git 打包和安装](eBook/09.9.md) + - 9.10 [Go 的外部包和项目](eBook/09.10.md) + - 9.11 [在 Go 程序中使用外部库](eBook/09.11.md) \ No newline at end of file diff --git a/eBook/09.10.md b/eBook/09.10.md index 7bfcf39..fcac0cc 100644 --- a/eBook/09.10.md +++ b/eBook/09.10.md @@ -2,39 +2,16 @@ 现在我们知道如何使用 Go 以及它的标准库了,但是 Go 的生态要比这大的多。当着手自己的 Go 项目时,最好先查找下是否有些存在的第三方的包或者项目不能使用。大多数可以通过 go install 来进行安装。 -第一个要去查看的地方是把项目用标签方式放在包展板上的 Go 项目网站 -(运行在Google App Engine): [https://godashboard.appspot.com/project](https://godashboard.appspot.com/project)。这是一个手动维护的记录表。 - -通过类别进行分类,例如编译工具,压缩工具,数据结构,数据库存储,开发工具等,包含了 500 个以上的项目,列出了每个项目的名字,对其简短的描述和下载链接。这些可以在下面的代码仓库中找到,()中为仓库所使用的代码控制系统。 - -- on Google Code, e.g. https://code.google.com/p/goprotobuf ,(Mercurial(hg) or Subversion) -- on Github: e.g. https://github.com/kr/pretty.go ,(Git) -- on BitBucket, e.g. https://bitbucket.org/binet/igo/ (Mercurial(hg)) -- on Launchpad, e.g. http://launchpad.net/mgo (Bazaar) - -或者在其他的知名的代码分享网站,或者作者的网站。 - -在库中你也可以在通过管理员审核后,提交您自己的项目。 - -如果你想看到实际的项目状态可以看看 go 项目网站上的包在包展板上的标签:[http://godashboard.appspot.com/package](http://godashboard.appspot.com/package)。 - -它给出一个“安装最多的包”(本周和所有时间)和最近安装包的概述,根据下载包的外部开发者的数量。如果编译栏的状态显示ok,这表示这个包是可以通过最新版本Go来安装的。Go项目和Go包页面是没有关系的。如果一个包出现在一边就没有必要出现在另一边。 - -其他整理资源(部分内容重叠): - -http://go-lang.cat-v.org/dev-utils (Developer-oriented) -http://go-lang.cat-v.org/go-code (Programs and applications) -http://go-lang.cat-v.org/library-bindings (Library bindings) -http://go-lang.cat-v.org/pure-go-libs (Pure Go libraries) +[Go Walker][https://gowalker.org] 支持根据包名在海量数据中查询。 目前已经有许多非常好的外部库,如: -* MySQL(GoMySQL), PostgreSQL(go-pgsql), MongoDB (mgo, gomongo), CouchDB (couch-go), ODBC (godbcl), Redis (redis.go) and SQLite3 (gosqlite) database drivers -* SDL bindings -* Google's Protocal Buffers(goprotobuf) -* XML-RPC(go-xmlrpc) -* Twitter(twitterstream) -* OAuth libraries(GoAuth) +- MySQL(GoMySQL), PostgreSQL(go-pgsql), MongoDB (mgo, gomongo), CouchDB (couch-go), ODBC (godbcl), Redis (redis.go) and SQLite3 (gosqlite) database drivers +- SDL bindings +- Google's Protocal Buffers(goprotobuf) +- XML-RPC(go-xmlrpc) +- Twitter(twitterstream) +- OAuth libraries(GoAuth) ## 链接 diff --git a/eBook/09.11.md b/eBook/09.11.md index a83e8c6..42a03d3 100644 --- a/eBook/09.11.md +++ b/eBook/09.11.md @@ -1,6 +1 @@ -# 9.11 在 Go 程序中使用外部库 (本节我们将创建一个 web 应用和它的 Google App Engine 版本,在第 19 和 21 章分别说明,当你阅读到这些章节时可以再回到这个例子。) 当开始一个新项目或增加新的功能到现有的项目,你可以通过在应用程序中使用已经存在的库来节省开发时间。为了做到这一点,你必须理解库的API(应用编程接口),那就是:库中有哪些方法可以调用,如何调用。你可能没有这个库的源代码,但作者肯定有记载的API以及详细介绍了如何使用它。 作为一个例子,我们将使用谷歌的 API 的 urlshortener 编写一个小程序:你可以尝试一下在 http://goo.gl/ 输入一个像 "http://www.destandaard.be" 这样的URL,你会看到一个像 "http://goo.gl/O9SUO" 这样更短的 URL 返回,也就是说,在 Twitter 之类的服务中这是非常容易嵌入的。谷歌 urlshortener 服务的文档可以在 "http://code.google.com/apis/urlshortener/" 找到。(第 19 章,我们将开发自己版本的 urlshortener)。 谷歌将这项技术提供给其他开发者,作为 API 我们可以在我们自己的应用程序中调用(释放到指定的限制。他们也生成了一个 Go 语言客户端库使其变得更容易。 备注:谷歌让通过使用 Google API Go 客户端服务的开发者生活变得更简单,Go 客户端程序自动生成于 Google 库的 JSON 描述。更多详情在 http://code.google.com/p/google-api-go-client/。 下载并安装 Go 客户端库: - 将通过 go install 实现。但是首先要验证环境变量中是否含有 GOPATH 变量,因为外部源码将被下载到$GOPATH/src目录下并被安装到$GOPATH/PKG/"machine_arch"/目录下。 我们将通过在终端调用以下命令来安装 API: go install google-api-go-client.google.com/hg/urlshortener/v1 go install将下载源码,编译并安装包 (在Linux Ubuntu下使用6g r60 9841安装是可以的,被安装文件被放在pkg/linux_amd64下) 使用urlshortener服务的web程序: 现在我们可以通过导入并赋予别名来使用已安装的包:import urlshortener "google-api-go-client.googlecode.com/hg/urlshortener/v1" 现在我们写一个web应用(参见第十五章4-8节)通过表单实现短地址和长地址的相互转换。我们将使用template包并写三个处理函数:root函数通过执行表单模板来展示表单。short函数将长地址转换为短地址,long函数逆向转换。 要调用urlshortener接口必须先通过http包中的默认客户端创建一个服务实例urlshortenerSvc: urlshortenerSvc, _ := urlshortener.New(http.DefaultClient) 我们通过调用服务中的Url.Insert中的Do方法传入包含长地址的Url数据结构从而获取短地址: url, _ := urlshortenerSvc.Url.Insert(&urlshortener.Url{LongUrl: longUrl}).Do() 返回url的Id便是我们需要的短地址。 我们通过调用服务中的Url.Get中的Do方法传入包含短地址的Url数据结构从而获取长地址: url, error := urlshortenerSvc.Url.Get(shwortUrl).Do() 返回的长地址便是转换前的原始地址。 实例 9.9 [urlshortener.go](examples/chapter_9/urlshortener.go) package main import ( “fmt” “net/http” “text/template” rlshortener “google-api-go-client.googlecode.com/hg/urlshortener/v1” ) func main() { http.HandleFunc(“/”, root) http.HandleFunc(“/short”, short) http.HandleFunc(“/long”, long) http.ListenAndServe(“localhost:8080”, nil) } // the template used to show the forms and the results web page to the user var rootHtmlTmpl = template.Must(template.New(“rootHtml”).Parse(`

URL SHORTENER

{{if .}}{{.}}

{{end}}
Shorten this:

Expand this: http://goo.gl/
`)) func root(w http.ResponseWriter, r *http.Request) { rootHtmlTmpl.Execute(w, nil) } func short(w http.ResponseWriter, r *http.Request) { longUrl := r.FormValue(“longUrl”) urlshortenerSvc, _ := urlshortener.New(http.DefaultClient) url, _ := urlshortenerSvc.Url.Insert(&urlshortener.Url{LongUrl: longUrl,}).Do() rootHtmlTmpl.Execute(w, fmt.Sprintf(“Shortened version of %s is : %s”, longUrl, url.Id)) } func long(w http.ResponseWriter, r *http.Request) { shortUrl := “http://goo.gl/” + r.FormValue(“shortUrl”) urlshortenerSvc, _ := urlshortener.New(http.DefaultClient) url, err := urlshortenerSvc.Url.Get(shortUrl).Do() if err != nil { fmt.Println(“error: %v”, err) return } rootHtmlTmpl.Execute(w, fmt.Sprintf(“Longer version of %s is : %s”, shortUrl, url.LongUrl)) } 编译这段代码: 6g -I $GOPATH/pkg/linux_amd64 urlshortener.go 链接这段代码: 6l -L $GOPATH/pkg/linux_amd64 urlshortener.6 执行这段代码:./6.out (确认是否有其他的应用运行在http://localhost:8080,如果有这步将失败)通过浏览http://localhost:8080/的页面来测试。 为了代码的简洁我们并没有检测返回的错误状态,但是在真实的生产环境的应用中一定要做检测。 将应用放入Google App Engine: 我们只需要在之前的代码中作出如下改变: package main -> package urlshort func main() -> func init() 创建一个和包同名的目录: urlshort 将以下两个安装目录复制到这个目录: google-api-go-client.googlecode.com/hg/urlshortener google-api-go-client.googlecode.com/hg/google-api 此外还要配置下配置文件app.yaml,内容如下: application: urlshort version: 0-1-test runtime: go api_version: 3 handlers: - url: /.* script: _go_app 现在你可以去到你的项目目录并在终端运行:dev_appserver.py urlshort 在浏览器打开你的web应用:http://localhost:8080 ## 链接 - -- [目录](directory.md) -- 上一节:[Go 的外部包和项目](09.10.md) -- 下一章:[结构(struct)与方法(method)](10.0.md) \ No newline at end of file +# 9.11 在 Go 程序中使用外部库 (本节我们将创建一个 Web 应用和它的 Google App Engine 版本,在第 19 和 21 章分别说明,当你阅读到这些章节时可以再回到这个例子。) 当开始一个新项目或增加新的功能到现有的项目,你可以通过在应用程序中使用已经存在的库来节省开发时间。为了做到这一点,你必须理解库的 API(应用编程接口),那就是:库中有哪些方法可以调用,如何调用。你可能没有这个库的源代码,但作者肯定有记载的 API 以及详细介绍了如何使用它。 作为一个例子,我们将使用谷歌的 API 的 urlshortener 编写一个小程序:你可以尝试一下在 http://goo.gl/ 输入一个像 "http://www.destandaard.be" 这样的URL,你会看到一个像 "http://goo.gl/O9SUO" 这样更短的 URL 返回,也就是说,在 Twitter 之类的服务中这是非常容易嵌入的。谷歌 urlshortener 服务的文档可以在 "http://code.google.com/apis/urlshortener/" 找到。(第 19 章,我们将开发自己版本的 urlshortener)。 谷歌将这项技术提供给其他开发者,作为 API 我们可以在我们自己的应用程序中调用(释放到指定的限制。他们也生成了一个 Go 语言客户端库使其变得更容易。 备注:谷歌让通过使用 Google API Go 客户端服务的开发者生活变得更简单,Go 客户端程序自动生成于 Google 库的 JSON 描述。更多详情在 [项目页面](http://code.google.com/p/google-api-go-client/) 查看。 下载并安装 Go 客户端库: 将通过 go install 实现。但是首先要验证环境变量中是否含有 `GOPATH` 变量,因为外部源码将被下载到 `$GOPATH/src` 目录下并被安装到 `$GOPATH/PKG/"machine_arch"/` 目录下。 我们将通过在终端调用以下命令来安装 API: go install google-api-go-client.google.com/hg/urlshortener/v1 go install 将下载源码,编译并安装包 使用 urlshortener 服务的 web 程序: 现在我们可以通过导入并赋予别名来使用已安装的包: `import urlshortener "google-api-go-client.googlecode.com/hg/urlshortener/v1"` 现在我们写一个 Web 应用(参见第 15 章 4-8 节)通过表单实现短地址和长地址的相互转换。我们将使用 `template` 包并写三个处理函数:root 函数通过执行表单模板来展示表单。short 函数将长地址转换为短地址,long 函数逆向转换。 要调用 urlshortener 接口必须先通过 http 包中的默认客户端创建一个服务实例 urlshortenerSvc: ```go urlshortenerSvc, _ := urlshortener.New(http.DefaultClient) ``` 我们通过调用服务中的 `Url.Insert` 中的 `Do` 方法传入包含长地址的 `Url` 数据结构从而获取短地址: ```go url, _ := urlshortenerSvc.Url.Insert(&urlshortener.Url{LongUrl: longUrl}).Do() ``` 返回 `url` 的 `Id` 便是我们需要的短地址。 我们通过调用服务中的 `Url.Get` 中的 `Do` 方法传入包含短地址的Url数据结构从而获取长地址: ```go url, error := urlshortenerSvc.Url.Get(shwortUrl).Do() ``` 返回的长地址便是转换前的原始地址。 示例 9.9 [urlshortener.go](examples/chapter_9/urlshortener.go) ```go package main import ( "fmt" "net/http" "text/template" rlshortener "google-api-go-client.googlecode.com/hg/urlshortener/v1" ) func main() { http.HandleFunc("/", root) http.HandleFunc("/short", short) http.HandleFunc("/long", long) http.ListenAndServe("localhost:8080", nil) } // the template used to show the forms and the results web page to the user var rootHtmlTmpl = template.Must(template.New("rootHtml").Parse(`

URL SHORTENER

{{if .}}{{.}}

{{end}}
Shorten this:

Expand this: http://goo.gl/
`)) func root(w http.ResponseWriter, r *http.Request) { rootHtmlTmpl.Execute(w, nil) } func short(w http.ResponseWriter, r *http.Request) { longUrl := r.FormValue("longUrl") urlshortenerSvc, _ := urlshortener.New(http.DefaultClient) url, _ := urlshortenerSvc.Url.Insert(&urlshortener.Url{LongUrl: longUrl,}).Do() rootHtmlTmpl.Execute(w, fmt.Sprintf("Shortened version of %s is : %s", longUrl, url.Id)) } func long(w http.ResponseWriter, r *http.Request) { shortUrl := "http://goo.gl/" + r.FormValue("shortUrl") urlshortenerSvc, _ := urlshortener.New(http.DefaultClient) url, err := urlshortenerSvc.Url.Get(shortUrl).Do() if err != nil { fmt.Println("error: %v", err) return } rootHtmlTmpl.Execute(w, fmt.Sprintf("Longer version of %s is : %s", shortUrl, url.LongUrl)) } ``` 执行这段代码: go run urlshortener.go 通过浏览 `http://localhost:8080/` 的页面来测试。 为了代码的简洁我们并没有检测返回的错误状态,但是在真实的生产环境的应用中一定要做检测。 将应用放入Google App Engine,我们只需要在之前的代码中作出如下改变: package main -> package urlshort func main() -> func init() 创建一个和包同名的目录 `urlshort`,并将以下两个安装目录复制到这个目录: google-api-go-client.googlecode.com/hg/urlshortener google-api-go-client.googlecode.com/hg/google-api 此外还要配置下配置文件 `app.yaml`,内容如下: ```yaml application: urlshort version: 0-1-test runtime: go api_version: 3 handlers: - url: /.* script: _go_app ``` 现在你可以去到你的项目目录并在终端运行:`dev_appserver.py urlshort` 在浏览器打开你的 Web应用:http://localhost:8080。 ## 链接 - [目录](directory.md) - 上一节:[Go 的外部包和项目](09.10.md) - 下一章:[结构(struct)与方法(method)](10.0.md) \ No newline at end of file