ch1/ch1-05 #175
Replies: 19 comments 2 replies
-
练习1.7,1.8,1.9 func testFetch1() {
for _, url := range os.Args[1:] {
if !strings.HasPrefix(url, "http://") {
url = "http://" + url
}
resp, err := http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr, "fetch: %v\n", err)
os.Exit(resp.StatusCode)
}
_, err = io.Copy(os.Stdout, resp.Body)
resp.Body.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "fetch: reading %s: %v\n", url, err)
os.Exit(1)
}
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
)
func main() {
for _, url := range os.Args[1:] {
//////exercise1.8
if !strings.HasPrefix(url, "http://") {
url = "http://" + url
}
resp, err := http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr, "fetch: %v\n", err)
os.Exit(1)
}
//////exercise1.7
if _, err := io.Copy((os.Stdout), resp.Body); err != nil {
log.Fatal(err)
}
//////exericise1.9 打印HTTP 协议的状态码
fmt.Printf("\n%s\n", resp.Status)
}
} |
Beta Was this translation helpful? Give feedback.
-
// Fetch prints the content found at a URL. import ( func main() { |
Beta Was this translation helpful? Give feedback.
-
1.7、1.8、1.9
|
Beta Was this translation helpful? Give feedback.
-
Exercises Answerpackage main
import (
"fmt"
"io"
"net/http"
"os"
"strings"
)
func main() {
for _, url := range os.Args[1:] {
// exercise 1.9
if !strings.HasPrefix(url, "http") {
url = "http://" + url
}
resp, err := http.Get(url)
fmt.Printf("status code: %v\n", resp.Status) // exercise 1.8
if err != nil {
fmt.Fprintf(os.Stderr, "fetch: %v\n", err)
os.Exit(1)
}
_, err = io.Copy(os.Stdout, resp.Body) // exercise 1.7
resp.Body.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "fetch: reading %s: %v\n", url, err)
os.Exit(1)
}
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
package main import ( func main() { |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
练习1.7 1.8 1.9func main() {
f, err := os.Create("resource/body.txt")
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "fetch: create file %v\n", err)
os.Exit(1)
}
for _, url := range os.Args[1:] {
if !strings.HasPrefix(url, "https://") {
url = "https://" + url
}
resp, err := http.Get(url)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "fetch: %v\n", err)
os.Exit(1)
}
_, err = io.Copy(f, resp.Body)
_ = resp.Body.Close()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "fetch: reading %s: %v\n", url, err)
os.Exit(1)
}
fmt.Println(url, resp.Status)
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
package main
import (
"net/http"
"os"
"io"
"fmt"
"strings"
)
func main() {
for _, url := range os.Args[1:] {
prefix := "http://"
if !strings.HasPrefix(prefix, url) {
url = prefix + url
}
resp, err := http.Get(url)
code := resp.StatusCode
if err != nil {
fmt.Fprintf(os.Stderr, "curl: %v\n", err)
os.Exit(code)
}
//当执行到defer语句时,defer后的函数调用不会立即执行,而是被放入到一个栈中。
//当包含defer语句的函数执行完毕时(即将返回),会按照后进先出(LIFO)的顺序执行这个栈中的所有defer调用。
defer resp.Body.Close()
_, err = io.Copy(os.Stdout, resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "curl reading: %v\n", err)
os.Exit(1)
}
fmt.Printf("%v\n", code)
}
} |
Beta Was this translation helpful? Give feedback.
-
// Fetch prints the content found at a URL. import ( func main() { |
Beta Was this translation helpful? Give feedback.
-
练习1: for _, url := range urls {
response, err := http.Get(url)
if err != nil || response.StatusCode != 200 {
log.Fatal("请求失败")
}
//流输出
_, err = io.Copy(os.Stdout, response.Body)
if err != nil {
log.Fatal("处理失败")
}
} |
Beta Was this translation helpful? Give feedback.
-
练习2: for _, url := range urls {
if !strings.HasPrefix(url, "http://") {
url = "http://" + url
}
response, err := http.Get(url)
if err != nil {
log.Fatal("请求失败")
}
_, err = io.Copy(os.Stdout, response.Body)
if err != nil {
log.Fatal("读取失败")
}
} 练习3 func test3(urls []string) {
for _, url := range urls {
response, err := http.Get(url)
if err != nil {
fmt.Println("请求失败")
}
_, err = io.Copy(os.Stdout, response.Body)
if err != nil {
fmt.Println("读取失败")
}
fmt.Println(response.Status)
_, err = io.Copy(os.Stdout, response.Body)
if err != nil {
fmt.Println("读取失败")
}
}
} |
Beta Was this translation helpful? Give feedback.
-
控制台输出版 for _, url := range os.Args[1:] {
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
url = "http://" + url
fmt.Fprintf(os.Stdout, "new Url: %v\n", url)
}
resp, err := http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr, "fetch: %v\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "http status code: %v\n", resp.StatusCode)
_, err = io.Copy(os.Stdout, resp.Body)
resp.Body.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "fetch: reading %s: %v\n", url, err)
os.Exit(1)
}
} |
Beta Was this translation helpful? Give feedback.
-
// Fetch prints the content found at a URL. import ( func main() { |
Beta Was this translation helpful? Give feedback.
-
打卡,并且表示忏悔,因为抄了评论第一的答案 |
Beta Was this translation helpful? Give feedback.
-
ch1/ch1-05
中文版
https://gopl-zh.github.io/ch1/ch1-05.html
Beta Was this translation helpful? Give feedback.
All reactions