- package main
-
- import (
- "github.com/gin-gonic/gin"
- "testgin/part03/myfunc"
- )
-
- func main() {
- r :=gin.Default()
-
- r.LoadHTMLGlob("part03/temp/**/*")
- r.GET("/userindex",myfunc.Hello1)
- r.POST("/getUserInfo",myfunc.Hello2)
-
-
-
- r.Run(":4444") //指定socket访问端口
-
-
- }
- package myfunc
-
- import (
- "fmt"
- "github.com/gin-gonic/gin"
- )
-
- func Hello1(context *gin.Context){
- //获取路径中的参数值
- context.HTML(200,"demo01/hello.html",nil)
-
- }
- func Hello2(context *gin.Context) {
- uname :=context.PostForm("username")
- pwd :=context.PostForm("pwd")
- fmt.Println(uname)
- fmt.Println(pwd)
- }
- {{define "demo01/hello.html" }}
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- <link rel="stylesheet" href="/s/css/mycss.css">
- head>
- <body>
- 定义一个用户的from表单:
- <form action="getUserInfo" method="post">
- 用户名:<input type="text" name="username">
- 密码: <input type="password" name="pwd">
- <input type="submit" value="提交">
- form>
- body>
- html>
- {{end}}
html页面form表单调用了post方法,路由是getUserInfo

表单提交后,服务器控制台收到了,post传递过来的表单信息