目的
1 golang 实现 http get 请求
2 对 web server 返回的 json 数据进行 struct 处理
html 返回信息如下
{
"sync": false,
"server": ["rsync1.vclound.com", "rsync2.vclound.com", "rsync3.vclound.com"],
"source": "0.0.0.0"
}
golang 代码如下
package main
import (
"fmt"
"net/http"
"io/ioutil"
"encoding/json"
)
// 先对 json 格式进行 struct 结构定义
type RsyncResponse struct {
Server []string `json:"server"`
Source string `json:"source"`
Sync bool `json:"sync"`
}
func httpget(url string) {
var body RsyncResponse
// 对 http 进行 get 请求
client := &http.Client{}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
}
response, _ := client.Do(request)
defer response.Body.Close()
responseB

该博客详细介绍了如何使用Golang进行HTTP GET请求,以及解析接收到的JSON响应数据,通过示例代码展示了具体的实现步骤。

1026

被折叠的 条评论
为什么被折叠?



