首页  

go程序设计语言01_05入门之获取URL     所属分类 go 浏览量 650
互联网信息获取 网络相关的包 
用网络收发信息,建立底层的网络连接,编写服务器程序
Go语言原生的并发特性

基于HTTP获取信息
curl工具

fetch.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 := ioutil.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)
    }
}

go build fetch.go

./fetch http://codefun007.xyz/
./fetch http://codefun007.xyz/pv.htm

./fetch http://codefun007.xy
fetch: Get "http://codefun007.xy": dial tcp: lookup codefun007.xy: no such host

上一篇     下一篇
mac安装xcode命令行工具

Brew 简介及安装

国货飞跃

go程序设计语言01_06入门之并发获取多个URL

GO箴言 goproverbs

简洁的GO语言