dsp1836 2015-10-21 08:37
浏览 13
已采纳

如何通过邮递员在Go lang中处理GET操作(CRUD)?

I want to perform a get operation. I am passng name as a resource to the URL. The URL I am hitting in Postman is : localhost:8080/location/{titan rolex} ( I chose the GET method in the dropdown list) On the URL hit in Postman, I am executing the GetUser func() with body as:

func GetUser(rw http.ResponseWriter, req *http.Request) {

}

Now I wish to get the resource value i.e 'titan rolex' in the GetUser method. How can I achieve this in golang?

In main(), I have this :

http.HandleFunc("/location/{titan rolex}", GetUser)

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doutan2111 2015-10-21 09:06
    关注

    What you are doing is binding the complete path /location/{titan rolex} to be handled by GetUser.

    What you really want is to bind /location/<every possible string> to be handled by one handler (e.g. LocationHandler).

    You can do that with either the standard library or another router. I will present both ways:

    1. Standard lib:

      import (
          "fmt"
          "net/http"
          "log"
      )
      
      func locationHandler(w http.ResponseWriter, r *http.Request) {
          name := r.URL.Path[len("/location/"):]
          fmt.Fprintf(w, "Location: %s
      ", name)
      }
      
      func main() {
          http.HandleFunc("/location/", locationHandler)
          log.Fatal(http.ListenAndServe(":8080", nil))
      }
      

      Note however, more complex paths (such as /location/<every possible string>/<some int>/<another string>) will be tedious to implement this way.

    2. The other way is to use github.com/julienschmidt/httprouter, especially if you encounter these situations more often (and have more complex paths).

      Here's an example for your use case:

      import (
          "fmt"
          "github.com/julienschmidt/httprouter"
          "net/http"
          "log"
      )
      
      func LocationHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
          fmt.Fprintf(w, "Location: %s
      ", ps.ByName("loc"))
      }
      
      func main() {
          router := httprouter.New()
          router.GET("/location/:loc", LocationHandler)
      
          log.Fatal(http.ListenAndServe(":8080", router))
      }
      

      Note that httprouter uses a slightly different signature for handlers. This is because, as you can see, it passes these parameters to the functions as well.

    Oh and another note, you can just hit http://localhost:8080/location/titan rolex with your browser (or something else) - if that something else is decent enough, it will URLEncode that to be http://localhost:8080/location/titan%20rolex.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?