go - 在 URL golang 中传递变量

标签 go

我是新来的,所以这可能是初级的。我有一个从 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/

相关文章:

http - 我应该避免在生产代码中使用 net/http/httptest 吗?

ios - 带有 "PIE disabled"/i386 arch 的 Gomobile 绑定(bind)生成库

rest - GoLang 通过 POST 请求发送文件

bash - 以 bash 友好的方式格式化 Go 日期

go - 如何从嵌套接口(interface)中获取值(value)

go - Concat在Golang中的两个 map

go - 如何在 Go 结构体标签内转义反引号

用于 concourse ci 任务的二进制文件

go - 是否可以在 go 插件和应用程序之间共享自定义数据类型?

go - Go 中的标志包 - 我是否必须始终设置默认值?