如何处理来自Go的请求方法,而不使用库?

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

how to handle request method from go. I don't use libraries

问题

在Go中,你可以使用http.HandleFunc函数来处理不同的HTTP方法。你可以创建多个处理函数,并将它们与相应的URL路径和HTTP方法进行关联。

以下是如何在Go中实现与你在Node.js中的示例相似的处理程序:

  1. package utils
  2. import (
  3. "log"
  4. "net/http"
  5. "github.com/user/go_rest_api/src/view" // 页面视图
  6. "github.com/user/go_rest_api/src/api" // 仅 API 服务器
  7. )
  8. func HandlerRequestFunc() {
  9. // 根路径处理函数
  10. http.HandleFunc("/", view.Homepage)
  11. // API 服务器处理函数
  12. http.HandleFunc("/api", api.InfoFromApi)
  13. // DB API 处理函数
  14. http.HandleFunc("/api/login", api.Login)
  15. http.HandleFunc("/api/registration", api.Registration)
  16. // 获取数据库列表处理函数
  17. http.HandleFunc("/api/db/list", api.DataBaseList)
  18. // 启动服务器
  19. log.Fatal(http.ListenAndServe(":3000", nil))
  20. }

在上面的示例中,我们使用http.HandleFunc函数将不同的URL路径与相应的处理函数进行关联。例如,"/"路径与view.Homepage处理函数关联,"/api"路径与api.InfoFromApi处理函数关联,以此类推。

请注意,这只是一个示例,你需要根据你的实际需求进行适当的修改和扩展。

英文:

before that I worked with node js and it was easy to understand how to handle methods

example on node js:

  1. switch (request.method) {
  2. case 'OPTIONS': return OptionsResponse(response);
  3. case 'GET': return GetSwitch(request, response);
  4. case 'POST': return PostSwitch(request, response);
  5. case 'PUT': return PutSwitch(request, response);
  6. case 'DELETE': return DeleteSwitch(request, response);
  7. default: return ErrorMessage( "Sorry, this method not supported", 501, request);
  8. }

but in go i can't figure out how to do it

  1. package utils
  2. import (
  3. "log"
  4. "net/http"
  5. "github.com/user/go_rest_api/src/view" // page view
  6. "github.com/user/go_rest_api/src/api" // only api server
  7. );

How can I implement the same handler in go?

  1. func HandlerRequestFunc() {
  2. http.HandleFunc("/", view.Homepage);
  3. /*get method from api server */
  4. http.HandleFunc("/api", api.InfoFromApi);
  5. /* from db api handler */
  6. http.HandleFunc("/api/login", api.Login);
  7. http.HandleFunc("/api/registration", api.Registration);
  8. /* get your list db */
  9. http.HandleFunc("/api/db/list", api.DataBaseList);
  10. /* server start function */
  11. log.Fatal(http.ListenAndServe(":3000", nil));

}

how not to process a method inside a function

答案1

得分: 0

以下是问题的翻译结果:

这是问题中的Node.js代码翻译成Go语言的代码。Go代码与Node.js代码非常相似。

  1. func myHandler(response http.ResponseWriter, request *http.Request) {
  2. switch request.Method {
  3. case "OPTIONS":
  4. OptionsResponse(response, request)
  5. case "GET":
  6. GetSwitch(response, request)
  7. case "POST":
  8. PostSwitch(response, request)
  9. case "PUT":
  10. PutSwitch(response, request)
  11. case "DELETE":
  12. DeleteSwitch(response, request)
  13. default:
  14. http.Error(response, "Sorry, this method not supported", 501)
  15. }
  16. }

请注意,这只是代码的翻译部分,不包括其他内容。

英文:

Here's the node.js code in the question translated to Go. The Go code is very similar to the node.js code.

  1. func myHandler(response http.ResponseWriter, request *http.Request) {
  2. switch request.Method {
  3. case "OPTIONS":
  4. OptionsResponse(response, request)
  5. case "GET":
  6. GetSwitch(response, request)
  7. case "POST":
  8. PostSwitch(response, request)
  9. case "PUT":
  10. PutSwitch(response, request)
  11. case "DELETE":
  12. DeleteSwitch(response, request)
  13. default:
  14. http.Error(response, "Sorry, this method not supported", 501)
  15. }
  16. }

答案2

得分: 0

我认为你需要为你的代码编写响应和请求,就像这样。因为这是该语言的默认包。

  1. func myHandler(w http.ResponseWriter, r *http.Request) {
  2. switch r.Method {
  3. case "OPTIONS":
  4. OptionsResponse(w, r)
  5. case "GET":
  6. GetSwitch(w, r)
  7. case "POST":
  8. PostSwitch(w, r)
  9. case "PUT":
  10. PutSwitch(w, r)
  11. case "DELETE":
  12. DeleteSwitch(w, r)
  13. default:
  14. http.Error(w, "抱歉,该方法不受支持", 501)
  15. }
  16. }

请注意,我已经将responserequest替换为了wr,以匹配函数签名中的参数名称。

英文:

i think you need wiriting response and request for your code
like this. because is the default package of the language

  1. func myHandler(w http.ResponseWriter, r *http.Request) {
  2. switch request.Method {
  3. case "OPTIONS":
  4. OptionsResponse(response, request)
  5. case "GET":
  6. GetSwitch(response, request)
  7. case "POST":
  8. PostSwitch(response, request)
  9. case "PUT":
  10. PutSwitch(response, request)
  11. case "DELETE":
  12. DeleteSwitch(response, request)
  13. default:
  14. http.Error(response, "Sorry, this method not supported", 501)
  15. }

}

答案3

得分: 0

也许这可以帮助你:

  1. type MyHandler struct{}
  2. func (self MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  3. }
  4. http.ListenAndServe(":3000", &MyHandler{})

这段代码定义了一个名为MyHandler的结构体,并实现了ServeHTTP方法。ServeHTTP方法接收一个http.ResponseWriter和一个http.Request参数,用于处理HTTP请求。

最后一行代码使用http.ListenAndServe函数启动一个HTTP服务器,监听在端口3000,并将&MyHandler{}作为处理器传入。

英文:

Maybe this can help you:

  1. type MyHandler struct{}
  2. func (self MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  3. }
  4. http.ListenAndServe(":3000", &MyHandler{})

huangapple
  • 本文由 发表于 2022年5月22日 22:56:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/72338686.html
匿名

发表评论

匿名网友

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

确定