package main import ( "github.com/gin-gonic/gin" ) func main() { router := gin.Default() router.POST("/form_post", func(c *gin.Context) { message := c.PostForm("username") nick := c.DefaultPostForm("userpassword", "????") // 此方法可以设置默认值 // gin.H 实际上就是 map[string]interface{} c.JSON(200, gin.H{ "status": "posted", "username": message, "userpassword": nick, }) }) router.Run(":8080") }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="http://localhost:8080/form_post" method="post" action="application/x-www-form-urlencoded"> 用户名:<input type="text" name="username" placeholder="请输入你的用户名"> <br> 密 码:<input type="password" name="userpassword" placeholder="请输入你的密码"> <br> <input type="submit" value="提交"> </form> </body> </html>