golanghttp.get 访问 web 方式及处理返回 json 信息方法

tech2026-02-16  1

目的

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)
最新回复(0)