亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

錯誤 GET 頁面請求

錯誤 GET 頁面請求

Go
慕的地8271018 2021-11-15 17:07:23
我是 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 回答

?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

總是處理你的錯誤

 t, _ := template.ParseFiles("login.gtpl")

如果這失敗了怎么辦?然后t是 nil 并且你得到同樣的錯誤


查看完整回答
反對 回復 2021-11-15
?
米脂

TA貢獻1836條經驗 獲得超3個贊

  1. 不要忽略從 template.ParseFiles 返回的錯誤

  2. 將包含目錄的路徑放入 template.ParseFiles


查看完整回答
反對 回復 2021-11-15
  • 2 回答
  • 0 關注
  • 248 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號