go gin form 表单验证 仅做记录

 

main.go 

package main

import (
	"fmt"
	"github.com/xiuno/gin"
	"html/template"
	"net/http"
	"time"
)

func formatAsDate(t time.Time) string {
	year, month, day := t.Date()
	return fmt.Sprintf("%d%02d/%02d", year, month, day)
}
// type Person struct {
// 	ID string `uri:"id" binding:"required,uuid"`
// 	Name string `uri:"name" binding:"required"`
// }

type myForm struct {
	Colors []string `form:"colors[]"`
}

func formHandler(c *gin.Context)  {
	var fakeForm myForm
	c.ShouldBind(&fakeForm)
	c.JSON(200, gin.H{"color":fakeForm.Colors})
}
func main()  {
	r := gin.Default()
	// r.GET("/:name/:id", func(c *gin.Context) {
	// 	var person Person
	// 	if err := c.ShouldBindUri(&person); err != err {
	// 		c.JSON(400, gin.H{"msg":err})
	// 		return
	// 	}
	// 	c.JSON(200, gin.H{"name":person.Name, "uuid": person.ID})
	// })
	// r.Delims("{[{", "}]}") //您可以使用自定义delims
	r.SetFuncMap(template.FuncMap{
		"formatAsDate": formatAsDate,
	})
	r.LoadHTMLGlob("view/*")
	r.GET("/", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.html", gin.H{
			"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
			"title": "hello Go",
		})
	})
	r.POST("/", formHandler)
	r.Run()
}

 

view/index.html




    
    
    
    {{ .title }}


Check some colors

 

你可能感兴趣的:(go)