gopl-zh.github.com/ch1/ch1-05.md

69 lines
3.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

## 1.5. 获取URL
对于很多现代应用来说访问互联网上的信息和访问本地文件系统一样重要。Go语言在net这个强大package的帮助下提供了一系列的package来做这件事情使用这些包可以更简单地用网络收发信息还可以建立更底层的网络连接编写服务器程序。在这些情景下Go语言原生的并发特性在第八章中会介绍显得尤其好用。
为了最简单地展示基于HTTP获取信息的方式下面给出一个示例程序fetch这个程序将获取对应的url并将其源文本打印出来这个例子的灵感来源于curl工具译注unix下的一个用来发http请求的工具具体可以man curl。当然curl提供的功能更为复杂丰富这里只编写最简单的样例。这个样例之后还会多次被用到。
<u><i>gopl.io/ch1/fetch</i></u>
```go
// Fetch prints the content found at a URL.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
)
func main() {
for _, url := range os.Args[1:] {
resp, err := http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr, "fetch: %v\n", err)
os.Exit(1)
}
b, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "fetch: reading %s: %v\n", url, err)
os.Exit(1)
}
fmt.Printf("%s", b)
}
}
```
这个程序从两个package中导入了函数net/http和iohttp.Get函数是创建HTTP请求的函数如果获取过程没有出错那么会在resp这个结构体中得到访问的请求结果。resp的Body字段包括一个可读的服务器响应流。io.ReadAll函数从response中读取到全部内容将其结果保存在变量b中。resp.Body.Close关闭resp的Body流防止资源泄露Printf函数会将结果b写出到标准输出流中。
```
$ go build gopl.io/ch1/fetch
$ ./fetch http://gopl.io
<html>
<head>
<title>The Go Programming Language</title>title>
...
```
HTTP请求如果失败了的话会得到下面这样的结果
```
$ ./fetch http://bad.gopl.io
fetch: Get http://bad.gopl.io: dial tcp: lookup bad.gopl.io: no such host
```
译注在大天朝的网络环境下很容易重现这种错误下面是Windows下运行得到的错误信息
```
$ go run main.go http://gopl.io
fetch: Get http://gopl.io: dial tcp: lookup gopl.io: getaddrinfow: No such host is known.
```
无论哪种失败原因我们的程序都用了os.Exit函数来终止进程并且返回一个status错误码其值为1。
**练习 1.7** 函数调用io.Copy(dst, src)会从src中读取内容并将读到的结果写入到dst中使用这个函数替代掉例子中的ioutil.ReadAll来拷贝响应结构体到os.Stdout避免申请一个缓冲区例子中的b来存储。记得处理io.Copy返回结果中的错误。
**练习 1.8** 修改fetch这个范例如果输入的url参数没有 `http://` 前缀的话为这个url加上该前缀。你可能会用到strings.HasPrefix这个函数。
**练习 1.9** 修改fetch打印出HTTP协议的状态码可以从resp.Status变量得到该状态码。