Regexp and named captures

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

I think I know the answer to this …I’ve been doing quite a bit of Go lately, it’s simply a side-effect of the nature of the language and maybe one day the regexp package will be changed around but until then we’ll have to transfer named args. to a map before we can use them conveniently.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.