How to get variables from a form post in Go-Gin?
In Go-Gin, you can use the c.PostForm method to retrieve form variables from a POST request. The method takes the key of the form variable as its argument and returns the value of the variable as a string.
Here’s an example of how you can use the c.PostForm method to retrieve the value of a form variable named “name”:
In this example, when a user submits a form with a field named “name”, the value of that field will be passed as an argument to the c.PostForm method, which will then assign the value to the variable “name”.
You can also use c.ShouldBind for more complex form structure, and the struct should have the binding:”required” tag on the field you want to be required.
In this example, the struct Form is being used to bind the form data to the struct fields, and the struct field ‘Name’ is marked as required, so if the form data doesn’t contain the ‘Name’ field, it will return a bad request error.