草庐IT

handleFunc

全部标签

go - 为什么http.HandleFunc对一个请求执行两次?

Thisquestionalreadyhasanswershere:HandleFuncbeingcalledtwice(3个答案)去年关闭。我用golang构建了一个非常简单的网络服务器,以了解http包,但是我发现HandleFunc函数对一个请求执行了两次,并且有一个favicon.ico没想到。这是Web服务器代码:packagemainimport("fmt""log""net/http""strings")//sayHelloNameabasicwebfunctionfuncsayHelloName(whttp.ResponseWriter,r*http.Request){

http - Golang http.HandleFunc:处理查询

同事!示例代码是标准的:funcecho(whttp.ResponseWriter,r*http.Request){c,_:=upgrader.Upgrade(w,r,nil)deferc.Close()for{_,message,_:=c.ReadMessage()log.Printf("recv:%s",message)ifstring(message)=="qwerty"{func(){fmt.Println("infunc1")time.Sleep(time.Second*10)//heremethodperformssomework}()}ifstring(message)=

json - 在 GoLang 中,如何获取 HandleFunc() 函数以将 json 解析为函数外部可访问的变量

我正在尝试使用golang创建一个服务,它将在一个端口上监听包含json的发布请求,并想解析出json的用户名和密码字段并将它们保存为变量以在函数外部使用对ActiveDirectory进行身份验证。我正在使用HandleFunc()函数,但无法确定如何访问函数外部的变量。我尝试创建一个返回,但它不会建立。如何正确创建变量然后在函数外部使用它们?packagemainimport("gopkg.in/ldap.v2""fmt""net/http""os""encoding/json""log""crypto/tls""html")typeMessagestruct{Userstring

go - 如何实现子路由

我想实现这样的路线用户/个人资料用户/购物车用户/产品目前,我正在做这件事r.HandleFunc("user/signup",signupHandler).Methods("POST")r.HandleFunc("user/signin",signinHandler).Methods("POST")r.HandleFunc("user/profile",profileHandler).Methods("GET")r.HandleFunc("user/cart",cartHandler).Methods("POST")r.HandleFunc("user/products",produ

go - Handle 和 HandleFunc 有什么区别?

这个问题在这里已经有了答案:Differencebetweenhttp.Handleandhttp.HandleFunc?(4个答案)关闭4年前。我试图了解Handle和HandleFunc之间的区别。除了差异之外,在构建Go网络应用程序时,您什么时候会使用一个而不是另一个?https://golang.org/pkg/net/http/#Handle

http - 如何将 http.HandleFunc 与 slugs 一起使用

我正在做一个Go项目。当我尝试将slugs与http.HandleFunc一起使用时,我收到“404页面未找到错误”。当我取出子弹时,我的路由再次工作。主要有:http.HandleFunc("/products/feedback/{slug}",AddFeedbackHandler)调用:varAddFeedbackHandler=http.HandlerFunc(func(whttp.ResponseWriter,r*http.Request){w.Write([]byte("ChecksOut"))})当我将路径替换为:http.HandleFunc("/products/fee

正则表达式在 Golang HandleFunc 函数中不起作用

我正在尝试根据Go中的模式重定向URL。如果我的URL包含“clientApi”,那么我将它发送到clientApiPointfunc,否则我将它发送到redirectApiPointfunc。我的handleRequest函数是funchandleRequest(){r:=mux.NewRouter()r.HandleFunc("/",homePage)r.HandleFunc("/clientApi",clientApiPoint)r.HandleFunc("/{^((?!clientApi).)*$}",redirectApiPoint)http.Handle("/",r)log

go - 异步文件写入 Golang 中的 http.HandleFunc

我刚刚开始学习围棋。我正在编写一个小型服务器应用程序,处理请求的函数(方法)(通过http.HandleFunc)写入一个文件——总是同一个文件。因为,据我所知,http.HandleFunc为每个请求启动一个新的goroutine,我担心文件写入可能最终会以某种方式相互干扰-通过相互阻塞或只是重叠。从实际输出来看,到目前为止还没有出现这个问题,但会不会出现,如果会出现,我该如何解决?这是我的代码的清理版本:packagemainimport("os""net/http")typeServicestruct{file*os.File}func(ser*Service)handleReq

gorilla mux 子路由器空路径

我想创建一个/user子路由器,如下所示user:=app.Router.PathPrefix("/user").Subrouter()user.HandleFunc("/create",(&controllers.User{c}).Create)user.HandleFunc("",(&controllers.User{c}).Create).Methods("POST")user.HandleFunc("",(&controllers.User{c}).FindAll).Methods("GET")user.HandleFunc("/{id}",(&controllers.User

go - http.HandleFunc 与静态文件

我正在构建一个网页。该页面应该能够处理不同的http方法(GET、POST...)。我的页面在技术上工作并处理每种类型的请求,但在GET请求的情况下(在根"/"处提供index.html路径),我的页面显示不正确。图像或css均未正确显示,可能是因为找不到这些文件。我注意到http.Handle在替换到我下面的server.go代码中时提供了比http.HandleFunc更好的结果,因为图像和css使用以下方法正确显示:http.FileServer(http.Dir("static"))http.Handle("/",http.StripPrefix("/",fs))以下是我的网络