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>