The Seven Year Itch.

MENU

Go学习之 - 请求博客主页

February 11, 2018 • Read: 819 • Go

自动检测网站编码

go get golang.org/x/net/html
gopm get -g -v golang.org/x/net/html

简单实现请求我的博客主页。并返回内容

package main

import (
    "net/http"
    "io/ioutil"
    "fmt"
)

func main() {
    res, err := http.Get("https://www.soo9s.com/")

    if err != nil {
        panic(err)
    }

    defer res.Body.Close()

    if res.StatusCode != http.StatusOK {
        fmt.Println("Error status:", res.StatusCode)
        return
    }
    content, err := ioutil.ReadAll(res.Body)

    fmt.Printf("%s\n", content)
}

编译Go

编译后的程序

运行成功后

Tags: None
Archives QR Code
QR Code for this page
Tipping QR Code