From 59a54f4c9eba396ad80ad5acbad0b93096b997a7 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Thu, 20 Jun 2019 17:29:47 -0700 Subject: [PATCH] example --- example_compile_test.go | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 example_compile_test.go diff --git a/example_compile_test.go b/example_compile_test.go new file mode 100644 index 0000000..2d2832b --- /dev/null +++ b/example_compile_test.go @@ -0,0 +1,42 @@ +package pathmatch_test + +import ( + "github.com/reiver/go-pathmatch" + + "fmt" +) + +func ExampleCompile() { + + pattern, err := pathmatch.Compile("/v1/users/{user_id}/contacts/{contact_type}") + if nil != err { + fmt.Printf("ERROR: %s\n", err) + return + } + + target := struct{ + UserID string `match:"user_id"` + ContactType string `match:"contact_type"` + }{} + + + var path = "/v1/users/123/contacts/e-mail" + + matched, err := pattern.MatchAndLoad(path, &target) + if nil != err { + fmt.Printf("ERROR: %s\n", err) + return + } + + if !matched { + fmt.Printf("The path %q did not match.", path) + return + } + + fmt.Printf("user_id = %q\n", target.UserID) + fmt.Printf("contact_type = %q\n", target.ContactType) + + // Output: + // user_id = "123" + // contact_type = "e-mail" +}