helpers.go 585 B

123456789101112131415161718192021222324252627282930313233
  1. package routes
  2. import (
  3. "net/http"
  4. "github.com/gorilla/mux"
  5. )
  6. func GetQueryParam(r *http.Request, name string, defaultValue ...string) string {
  7. if v := r.URL.Query().Get(name); v != "" {
  8. return v
  9. } else {
  10. if len(defaultValue) == 0 {
  11. return ""
  12. } else {
  13. return defaultValue[0]
  14. }
  15. }
  16. }
  17. // get path segment. no idea why it's called "params"
  18. func GetPathVar(r *http.Request, name string, defaultValue ...string) string {
  19. if v := mux.Vars(r)[name]; v != "" {
  20. return v
  21. } else {
  22. if len(defaultValue) == 0 {
  23. return ""
  24. } else {
  25. return defaultValue[0]
  26. }
  27. }
  28. }