how to store value of form data store in some variable in golang

huangapple go评论94阅读模式
英文:

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
	}
}

how to store value of form data store in some variable in golang

英文:

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
	}
}

how to store value of form data store in some variable in golang

huangapple
  • 本文由 发表于 2022年4月14日 15:26:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/71867893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定