package main import ( "net/http" "github.com/gin-gonic/gin" ) type SprayEquipment struct { Speed1 int // 电磁阀1对应的喷雾量(百分比): 0-100 Speed2 int // 电磁阀2对应的喷雾量(百分比): 0-100 YV1 int // 电磁阀1的状态: 0 停止, 1 启动 YV2 int // 电磁阀2的状态: 0 停止, 1 启动 } func main() { r := gin.Default() r.POST("/plc", Plc) r.Run() } func Plc(c *gin.Context) { var json struct { Key1 *int `json:"key1" binding:"required"` Speed1 *int `json:"speed1" binding:"required_if=Key1 2"` // 喷雾量(百分比): 0-100 Speed2 *int `json:"speed2" binding:"required_if=Key1 2"` // 喷雾量(百分比): 0-100 YV1 *int `json:"yv1" binding:"required_if=Key1 2"` // 电磁阀1 状态 YV2 *int `json:"yv2" binding:"required_if=Key1 2"` // 电磁阀2 状态 } if err := c.ShouldBindJSON(&json); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "ok"}) }