4
0

route.go 741 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package route
  5. import (
  6. "log"
  7. "strconv"
  8. "github.com/gorilla/mux"
  9. )
  10. func GetRoute(router *mux.Router, name string, args ...interface{}) string {
  11. route := router.Get(name)
  12. if route == nil {
  13. log.Fatalln("Route not found:", name)
  14. }
  15. var pairs []string
  16. for _, param := range args {
  17. switch param.(type) {
  18. case string:
  19. pairs = append(pairs, param.(string))
  20. case int64:
  21. val := param.(int64)
  22. pairs = append(pairs, strconv.FormatInt(val, 10))
  23. }
  24. }
  25. result, err := route.URLPath(pairs...)
  26. if err != nil {
  27. log.Fatalln(err)
  28. }
  29. return result.String()
  30. }