You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
golang-tutorial/demo/http_request.go

47 lines
976 B

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.

package main
import (
"fmt"
"io/ioutil"
"net/http"
)
var (
url1 = "https://www.tinywan.com/api/open/returnUrl"
url2 = "https://restapi.amap.com/v3/weather/weatherInfo?key=d2315f3b0b4e57bbf5428e755a73e692&city=110101"
)
func main() {
res, err := doHttpGetRequest(url2)
if err != nil {
fmt.Println("net req error")
} else {
fmt.Println(res)
}
}
// http get请求函数
// 输入参数请求url
// 返回值res天气数据。err错误信息
func doHttpGetRequest(url string) (res string, err error) {
// http.Get在net/http中所以要import "net/http"
resp, err := http.Get(url)
if err != nil {
return "", err
} else {
// 使用efer resp.Body.Close()。当doHttpGetRequest成功return之后执行此行语句。多用于句柄关闭
defer resp.Body.Close()
// io流数据读取。需要引用io/ioutil
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
} else {
return string(body), err
}
}
}