在golang中使用路径变量调用GET REST API

huangapple go评论61阅读模式
英文:

Calling a GET REST API in golang with a path variable

问题

我第一次尝试使用Golang。我正在尝试调用一个带有路径变量的GET REST API。我正在使用net/http库。我尝试了下面的代码,但是到目前为止没有成功。我想知道如何使用路径变量并从代码中传递变量。非常感谢任何帮助或代码示例。

这段代码似乎不起作用:

http.Get("http://127.19.0.1:8080/student/:id")
英文:

I am trying Golang for the first time. I am trying to call a GET REST API which has a path variable. I am using net/http for that. I am trying like below but no luck so far. I need to know how I can use the path variable and pass the variable from the code. Any help or code example would be highly appreciated.

This does not seem to work:

http.Get("http://127.19.0.1:8080/student/:id")

答案1

得分: 3

关于这个问题的翻译如下:

http.Get(fmt.Sprintf("http://127.19.0.1:8080/student/%s", id)

是什么意思?

英文:

what about

http.Get(fmt.Sprintf("http://127.19.0.1:8080/student/%s", id)

?

huangapple
  • 本文由 发表于 2022年4月26日 22:45:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/72016005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定