- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
代码
package main
import (
"fmt"
"log"
"net/http"
"github.com/goji/httpauth"
)
func rootHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
data := "TEST"
w.Header().Set("Content-Length", fmt.Sprint(len(data)))
fmt.Fprint(w, string(data))
}
func main() {
r:=http.HandlerFunc(rootHandler)
http.HandleFunc("/", httpauth.SimpleBasicAuth("dave", "somepassword")(r))
log.Fatal(http.ListenAndServe(":8080", nil))
}
返回
:!go build test.go
# command-line-arguments
./test.go:23:71: cannot use httpauth.SimpleBasicAuth("dave", "somepassword")(r) (type http.Handler) as type func(http.ResponseWriter, *http.Request) in argument to http.HandleFunc
我做错了什么?我是 Go 的新手,不明白为什么这个例子 here不完整,不包含 YourHandler
函数。
最佳答案
在我的头撞到众所周知的墙上之后,我想通了。
使用http.Handle
,而不是http.HandleFunc
! :)
所以 main
函数为
func main() {
r:=http.HandlerFunc(rootHandler)
http.Handle("/", httpauth.SimpleBasicAuth("dave", "somepassword")(r))
log.Fatal(http.ListenAndServe(":8080", nil))
}
您有一个使用 net/http 的 httpauth 的完整工作示例!
This answer对内部工作原理也有非常好的概述。
关于goji/httpauth - 不能与标准 Golang 'net/http' 库一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46990556/
代码如下 package main import ( "fmt" "net/http" "github.com/zenazn/goji" "github.com/zen
我在从 goji 导入包时注意到 - 它直接托管在他们的域之外,例如: package web import ( "goji.io" ) func Start() { mux := g
我正在使用 Goji ( https://github.com/zenazn/goji ) 并希望定义具有自己的中间件的路由组。例如,/company 下的所有路径都应使用 LDAP 身份验证并定义一
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
我想映射每个路由及其请求类型(GET、POST、PUT 等),以便为我的 restful API 生成类似于 JSON 格式的 sitemap.xml。 Goji 使用函数创建新路线。我可以将路径和处
代码 package main import ( "fmt" "log" "net/http" "github.com/goji/htt
请看一下我的两个文件 main.go 和 index.html。我从 Goji 网络微框架开始。在 Goji web framework. 中查找片段 在我的示例中,我可以在@Localhost800
我有一个使用 Goji 的 Google App Engine 应用程序并定义了以下路由: func init() { mux := web.New() http.Handle("/a
我想用 Goji 和 Google App Engine/Go 开发应用程序。 我复制并粘贴了示例代码 https://github.com/zenazn/goji并将函数名称从“main”更改为“i
对于单个 Go 网络应用程序(使用 Goji)来说,这是处理 http 和 https 流量的正确方法吗? package main import ( "fmt" "net/http"
我是一名优秀的程序员,十分优秀!