我是新来的,所以这可能是初级的。我有一个从 URL 检索 json 的函数,需要在 URL 中传递一个可变整数。如何将变量附加到另一个变量的末尾?这是我的代码:
type content struct {
StationTitle string `json:"StationTitle"`
}
func main() {
resp := content{}
getContent("http://foo.foo2.foo3=variableInteger", &resp)
println(resp.StationTitle)
}
// fetch json
func getContent(url string, target interface{}) error {
r, err := http.Get(url)
if err != nil {
return err
}
defer r.Body.Close()
return json.NewDecoder(r.Body).Decode(target)
}
最佳答案
使用fmt.Sprintf
getContent(fmt.Sprintf("http://foo.foo2.foo3=%d", variableInteger), &resp)
关于go - 在 URL golang 中传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34458176/