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

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

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

问题

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

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

package utils

import (
	"log"
	"net/http"

	"github.com/user/go_rest_api/src/view" // 页面视图
	"github.com/user/go_rest_api/src/api" // 仅 API 服务器
)

func HandlerRequestFunc() {
	// 根路径处理函数
	http.HandleFunc("/", view.Homepage)

	// API 服务器处理函数
	http.HandleFunc("/api", api.InfoFromApi)

	// DB API 处理函数
	http.HandleFunc("/api/login", api.Login)
	http.HandleFunc("/api/registration", api.Registration)

	// 获取数据库列表处理函数
	http.HandleFunc("/api/db/list", api.DataBaseList)

	// 启动服务器
	log.Fatal(http.ListenAndServe(":3000", nil))
}

在上面的示例中,我们使用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:

    switch (request.method) {

    case 'OPTIONS': return OptionsResponse(response);
    case 'GET': return GetSwitch(request, response);
    case 'POST': return PostSwitch(request, response);
    case 'PUT': return PutSwitch(request, response);
    case 'DELETE': return DeleteSwitch(request, response);
    default: return ErrorMessage( "Sorry, this method not supported", 501, request);

}

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

package utils

import (

	"log"
	"net/http"

	"github.com/user/go_rest_api/src/view" // page view

	"github.com/user/go_rest_api/src/api" // only api server
);

How can I implement the same handler in go?

func HandlerRequestFunc() {

http.HandleFunc("/", view.Homepage);

/*get method from api server */
http.HandleFunc("/api", api.InfoFromApi);

/* from db api handler */
http.HandleFunc("/api/login", api.Login);
http.HandleFunc("/api/registration", api.Registration);

/* get your list db */
http.HandleFunc("/api/db/list", api.DataBaseList);

/* server start function */
log.Fatal(http.ListenAndServe(":3000", nil));

}

how not to process a method inside a function

答案1

得分: 0

以下是问题的翻译结果:

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

func myHandler(response http.ResponseWriter, request *http.Request) {
    switch request.Method {
    case "OPTIONS":
        OptionsResponse(response, request)
    case "GET":
        GetSwitch(response, request)
    case "POST":
        PostSwitch(response, request)
    case "PUT":
        PutSwitch(response, request)
    case "DELETE":
        DeleteSwitch(response, request)
    default:
        http.Error(response, "Sorry, this method not supported", 501)
    }
}

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

英文:

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

func myHandler(response http.ResponseWriter, request *http.Request) {
	switch request.Method {
	case "OPTIONS":
		OptionsResponse(response, request)
	case "GET":
		GetSwitch(response, request)
	case "POST":
		PostSwitch(response, request)
	case "PUT":
		PutSwitch(response, request)
	case "DELETE":
		DeleteSwitch(response, request)
	default:
		http.Error(response, "Sorry, this method not supported", 501)
	}
}

答案2

得分: 0

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

func myHandler(w http.ResponseWriter, r *http.Request) {
    switch r.Method {
    case "OPTIONS":
        OptionsResponse(w, r)
    case "GET":
        GetSwitch(w, r)
    case "POST":
        PostSwitch(w, r)
    case "PUT":
        PutSwitch(w, r)
    case "DELETE":
        DeleteSwitch(w, r)
    default:
        http.Error(w, "抱歉,该方法不受支持", 501)
    }
}

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

英文:

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

func myHandler(w http.ResponseWriter, r *http.Request) {
switch request.Method {
case "OPTIONS":
    OptionsResponse(response, request)
case "GET":
    GetSwitch(response, request)
case "POST":
    PostSwitch(response, request)
case "PUT":
    PutSwitch(response, request)
case "DELETE":
    DeleteSwitch(response, request)
default:
    http.Error(response, "Sorry, this method not supported", 501)
}

}

答案3

得分: 0

也许这可以帮助你:

type MyHandler struct{}

func (self MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {

}

http.ListenAndServe(":3000", &MyHandler{})

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

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

英文:

Maybe this can help you:

type MyHandler struct{}

func (self MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {

}

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:

确定