MCPcopy
hub / github.com/gorilla/mux / ExampleRoute_GetVarNames

Function ExampleRoute_GetVarNames

example_route_vars_test.go:10–35  ·  view source on GitHub ↗

This example demonstrates building a dynamic URL using required vars and values retrieve from another source

()

Source from the content-addressed store, hash-verified

8// This example demonstrates building a dynamic URL using
9// required vars and values retrieve from another source
10func ExampleRoute_GetVarNames() {
11 r := mux.NewRouter()
12
13 route := r.Host("{domain}").
14 Path("/{group}/{item_id}").
15 Queries("some_data1", "{some_data1}").
16 Queries("some_data2_and_3", "{some_data2}.{some_data3}")
17
18 dataSource := func(key string) string {
19 return "my_value_for_" + key
20 }
21
22 varNames, _ := route.GetVarNames()
23
24 pairs := make([]string, 0, len(varNames)*2)
25
26 for _, varName := range varNames {
27 pairs = append(pairs, varName, dataSource(varName))
28 }
29
30 url, err := route.URL(pairs...)
31 if err != nil {
32 panic(err)
33 }
34 fmt.Println(url.String())
35}

Callers

nothing calls this directly

Calls 6

HostMethod · 0.95
NewRouterFunction · 0.92
GetVarNamesMethod · 0.80
URLMethod · 0.80
QueriesMethod · 0.45
PathMethod · 0.45

Tested by

no test coverage detected