编码
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 新手,不明白为什么这里的示例不完整且不包含YourHandler
函数。