當我查看如何讀取表單數據的示例時,我遇到了兩種讀取表單值的方法:使用r.PostFormValue()username := r.PostFormValue("username")
password := r.PostFormValue("password")使用r.PostForm.Get()username := r.PostForm.Get("username")
password := r.PostForm.Get("password")為什么要使用一個而不是另一個?
1 回答

犯罪嫌疑人X
TA貢獻2080條經驗 獲得超4個贊
兩者都Request.PostFormValue()
返回Request.PostForm.Get()
相同的值,主要區別在于Request.PostForm
不會自動填充。
Request.PostForm
是表單數據的映射,通過調用Request.ParseMultipartForm()
or填充Request.ParseForm()
。這不會自動發生,因為這需要讀取和解析請求正文,而這可能并非在所有情況下都需要。
Request.PostFormValue()
調用ParseMultipartForm()
并ParseForm()
在必要時(如果之前未調用過)確保Request.PostForm
已填充。Request.PostForm
是一個表示's字段的選擇器,因此,它不涉及調用. 它假設你已經這樣做了。如果沒有,任何調用都將“靜默”返回一個空字符串。Request
PostForm
ParseForm()
PostForm.Get()
所以你應該只Request.PostForm.Get()
在你已經解析過表單數據的情況下使用(例如通過顯式調用Request.ParseForm()
或間接通過先前的Request.PostFormValue()
調用)。
- 1 回答
- 0 關注
- 137 瀏覽
添加回答
舉報
0/150
提交
取消