اتصال چکباکسهای HTML
چکباکسهای HTML با ویژگی name یکسان، هنگام انتخاب شدن مقادیر متعددی ارسال میکنند. Gin میتواند این مقادیر را مستقیماً با استفاده از تگ form در struct به یک اسلایس []string متصل کند.
این برای فرمهایی مفید است که کاربران یک یا چند گزینه انتخاب میکنند — مانند انتخابگر رنگ، انتخابگر دسترسی، یا فیلترهای چندگزینهای.
package main
import ( "net/http"
"github.com/gin-gonic/gin")
type myForm struct { Colors []string `form:"colors[]"`}
func main() { router := gin.Default()
router.LoadHTMLGlob("templates/*")
router.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "form.html", nil) })
router.POST("/", func(c *gin.Context) { var fakeForm myForm if err := c.ShouldBind(&fakeForm); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"color": fakeForm.Colors}) })
router.Run(":8080")}فرم HTML مربوطه (templates/form.html):
<form action="/" method="POST"> <p>Check some colors</p> <label for="red">Red</label> <input type="checkbox" name="colors[]" value="red" id="red"> <label for="green">Green</label> <input type="checkbox" name="colors[]" value="green" id="green"> <label for="blue">Blue</label> <input type="checkbox" name="colors[]" value="blue" id="blue"> <input type="submit"></form>تست
# Select all three colorscurl -X POST http://localhost:8080/ \ -d "colors[]=red&colors[]=green&colors[]=blue"# Output: {"color":["red","green","blue"]}
# Select only one colorcurl -X POST http://localhost:8080/ \ -d "colors[]=green"# Output: {"color":["green"]}
# No checkboxes selected -- slice is emptycurl -X POST http://localhost:8080/# Output: {"color":[]}