-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**Summary of Changes** 1. Added `r.GetVarNames()` function to list all vars a route might need in order to call `r.URL()` --------- Co-authored-by: Anonymous <[email protected]> Co-authored-by: Corey Daley <[email protected]>
- Loading branch information
1 parent
85123bf
commit 4a671cb
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package mux_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gorilla/mux" | ||
) | ||
|
||
// This example demonstrates building a dynamic URL using | ||
// required vars and values retrieve from another source | ||
func ExampleRoute_GetVarNames() { | ||
r := mux.NewRouter() | ||
|
||
route := r.Host("{domain}"). | ||
Path("/{group}/{item_id}"). | ||
Queries("some_data1", "{some_data1}"). | ||
Queries("some_data2_and_3", "{some_data2}.{some_data3}") | ||
|
||
dataSource := func(key string) string { | ||
return "my_value_for_" + key | ||
} | ||
|
||
varNames, _ := route.GetVarNames() | ||
|
||
pairs := make([]string, 0, len(varNames)*2) | ||
|
||
for _, varName := range varNames { | ||
pairs = append(pairs, varName, dataSource(varName)) | ||
} | ||
|
||
url, err := route.URL(pairs...) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(url.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters