Last active
August 27, 2015 12:41
-
-
Save nbari/7cedf18ae355fa0480a5 to your computer and use it in GitHub Desktop.
dynamic map
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
package my_package | |
import ( | |
"fmt" | |
"errors" | |
"regexp" | |
"strings" | |
) | |
type dynamicSet map[string]regexp.Regexp | |
func (d dynamicSet) Set(name string, regex string) error { | |
if !strings.HasPrefix(name, ":") { | |
return errors.New("Dynamic route name must start with a colon ':'") | |
} | |
// fix regex | |
if !strings.HasPrefix(regex, "^") { | |
regex = fmt.Sprintf("^%s$", regex) | |
} | |
r := regexp.MustCompile(regex) | |
d[name] = *r | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment