and use the following strategy to sanitize our input:
1 2 3 4 5 6 7 8
slice := []string{"apple", "pear", "banana"}
for _, v := range slice { if v == r.Form.Get("fruit") { return true } } return false
6. Radio buttons
1 2
<input type = "radio" name="gender" value="1">Male <input type = "radio" name="gender" value="2">Female
use the following code to validate the input:
1 2 3 4 5 6 7 8
slice := []int{1,2}
for _, v := range slice { if v == r.Form.Get("gender") { return true } } return false
7. Check boxes
1 2 3
<input type="checkbox" name = "interest" value = "football">football <input type="checkbox" name="interest" value="basketball">Basketball <input type="checkbox" name="interest" value="tennis">Tennis
In this case, the sanitization is a little bit different to validating the button and check box inputs since here we get a slice from the check boxes.
1 2 3 4 5 6 7
slice :=[]string{"football","basketball","tennis"} a := Slice_diff(r.Form["interest"],slice) if a == nil { return true }
return false
8. Date and time
Suppose you want users to input valid dates or times. Go has the time package for converting year, month and day to their corresponding times. After that, it’s easy to check it.
1 2
t := time.Date(2019, time.December, 10, 23, 0, 0, 0, time.UTC) fmt.Printf("Go launched at %s\n", t.Local())
After you have the time, you can use the time package for more operations, depending on needs.