I am just checking, is it necessary like I have below, to loop and add results into a map to be able to use named regex captures? I’m relatively new to Go and was a little surprised that a loop would be needed to do this as Go seems to be all about efficiency. I guess it could argued that named captures are slower than non-named captures so they are discouraged but I’m going to guess that there is something more fundamental that I should learn.
func getNamedStringSubmatches(re *regexp.Regexp, subject string) map[string]string {
matches := re.FindStringSubmatch(subject)
namedMatches := make(map[string]string)
namedMatches[`0`] = matches[0] // So we have the full match in case we need it.
for i, name := range re.SubexpNames() {
if name != `` {
namedMatches[name] = matches[i]
}
}
return namedMatches
}