In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how golang realizes http request weather, the introduction in the article is very detailed, has certain reference value, interested friends must read it!
I. Preparation:
1. Register a developer account and apply for a key
2, golang development tools LiteIDE (can be Baidu).
Or www.jb51.net/softjc/119086.html
3. Understand the basic grammar of golang.
Second, direct code
Because the code comments are very detailed, so directly on the code.
package mainimport ( "fmt" "io/ioutil" "net/http")func main() { rlt, err := doHttpGetRequest("https://restapi.amap.com/v3/weather/weatherInfo? key= Your Gaode key&city=110101") if err != nil { fmt.Println("net req error") } else { fmt.Println(rlt) }}//custom http get request function//input parameter: request url//return value: rlt, weather data. err, error message//network request func doHttpGetRequest(url string) (rlt string, err error) { // http.Get in net/http, so import "net/http" resp, err := http.Get(url) if err != nil { return "", err } else { //Use efer resp.Body.Close(). When doHttpGetRequest returns successfully, execute this line of statements. More for handle closure defer resp.Body.Close() // io Stream data read. Need to quote io/ioutil body, err := ioutil.ReadAll(resp.Body) if err != nil { return "", err } else { return string(body), err } }}
Note: My Gaode key: d2315f3b0b4e57bbf5428e755a73e692. For testing purposes
Return Results:
test results
That's all for "golang how to implement http request weather," thanks for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.