我试图在我的项目中链接处理程序。
package main
import (
"database/sql"
"fmt"
"net/http"
)
func loggingHandler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func loginHandler(res http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
http.ServeFile(res, req, "login.html")
return
}
// some code
}
func MainPageHandler(res http.ResponseWriter, req *http.Request) {
// some code
}
func main() {
db, err = sql.Open("", "password@()/db name")
if err != nil {
fmt.Println(err)
}
defer db.Close()
commonHandlers := alice.New(loggingHandler)
http.Handle("/Login", commonHandlers.ThenFunc(loginHandler))
http.Handle("/", commonHandlers.ThenFunc(MainPageHandler))
http.ListenAndServe(":8080", nil)
}
我该怎么做才能获得输出? 提前谢谢。
答案 0 :(得分:0)
请求处理程序可以通过传递http.ResponseWriter
作为第一个参数,使用任何fmt
Fprint*
methods来编写响应。
例如:
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "URL.Path = %q\n", r.URL.Path)
}