Skip to content

Instantly share code, notes, and snippets.

@nbari
Last active August 27, 2015 12:41
Show Gist options
  • Save nbari/7cedf18ae355fa0480a5 to your computer and use it in GitHub Desktop.
Save nbari/7cedf18ae355fa0480a5 to your computer and use it in GitHub Desktop.
dynamic map
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