example_test.go 898 B

123456789101112131415161718192021222324252627282930313233343536
  1. package binding
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. )
  7. type Arg struct {
  8. Max int64 `form:"max" validate:"max=10"`
  9. Min int64 `form:"min" validate:"min=2"`
  10. Range int64 `form:"range" validate:"min=1,max=10"`
  11. // use split option to split arg 1,2,3 into slice [1 2 3]
  12. // otherwise slice type with parse url.Values (eg:a=b&a=c) default.
  13. Slice []int64 `form:"slice,split" validate:"min=1"`
  14. }
  15. func ExampleBinding() {
  16. req := initHTTP("max=9&min=3&range=3&slice=1,2,3")
  17. arg := new(Arg)
  18. if err := Form.Bind(req, arg); err != nil {
  19. log.Fatal(err)
  20. }
  21. fmt.Printf("arg.Max %d\narg.Min %d\narg.Range %d\narg.Slice %v", arg.Max, arg.Min, arg.Range, arg.Slice)
  22. // Output:
  23. // arg.Max 9
  24. // arg.Min 3
  25. // arg.Range 3
  26. // arg.Slice [1 2 3]
  27. }
  28. func initHTTP(params string) (req *http.Request) {
  29. req, _ = http.NewRequest("GET", "http://api.bilibili.com/test?"+params, nil)
  30. req.ParseForm()
  31. return
  32. }