英文:
how to store value of form data store in some variable in golang
问题
r.ParseMultipartForm(0)
parent := r.Form.Get("parent")
fmt.Println(parent)
Path = "/" + parent + "" + name
fmt.Println(Path)
父级打印为空。请问有人可以告诉我如何将表单值存储到变量中吗?
英文:
r.ParseMultipartForm(0)
parent := r.Form.Get("parent")
fmt.Println(parent)
Path = "/" + parent + "" + name``
fmt.Println(Path)
parent print nothing.Could anyone please tell me how to store a form value to a variable
答案1
得分: 1
如果你正在使用net/http包:
package main
import (
"fmt"
"log"
"net/http"
)
func login(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Fatal(err)
}
email := r.PostForm.Get("email")
password := r.PostForm.Get("password")
finalValue := fmt.Sprintf("%s 和 %s", email, password)
fmt.Fprintln(w, finalValue)
}
func main() {
http.HandleFunc("/login", login)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("启动HTTP服务器时出错:", err)
return
}
}
英文:
If you are using net/http
package main
import (
"fmt"
"log"
"net/http"
)
func login(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Fatal(err)
}
// fmt.Fprintln(w, "Email : ", r.PostForm.Get("email"))
// fmt.Fprintln(w, "Password : ", r.PostForm.Get("password"))
email := r.PostForm.Get("email")
password := r.PostForm.Get("password")
finalValue := fmt.Sprintf("%s and %s", email, password)
// fmt.Println(finalValue)
fmt.Fprintln(w, finalValue)
}
func main() {
http.HandleFunc("/login", login)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("Error Starting the HTTP Server : ", err)
return
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论