我是 Golang 的新手,我正在尝试创建一个简单的 Web 服务器,但在 GET 请求中出现错误。这是代码:import ( "fmt" "html/template" "log" "net/http" "strings")func sayhelloName(w http.ResponseWriter, r *http.Request) { r.ParseForm() fmt.Println(r.Form) // print information on server side. fmt.Println("path", r.URL.Path) fmt.Println("scheme", r.URL.Scheme) fmt.Println(r.Form["url_long"]) for k, v := range r.Form { fmt.Println("key:", k) fmt.Println("val:", strings.Join(v, "")) } fmt.Fprintf(w, "Hello Worrld!") // write data to response}func login(w http.ResponseWriter, r *http.Request) { fmt.Println("method:", r.Method) //get request method if r.Method == "GET" { t, _ := template.ParseFiles("login.gtpl") t.Execute(w, nil) } else { r.ParseForm() // logic part of log in fmt.Println("username:", r.Form["username"]) fmt.Println("password:", r.Form["password"]) }}func main() { http.HandleFunc("/", sayhelloName) // setting router rule http.HandleFunc("/login", login) err := http.ListenAndServe(":9090", nil) // setting listening port if err != nil { log.Fatal("ListenAndServe: ", err) }}
- 2 回答
- 0 关注
- 214 浏览
添加回答
举报
0/150
提交
取消