Go

Go

Made by DeepSource

Using regexp.Match or related in a loop SCC-SA6000

Performance
Major

Compile parses a regular expression and returns, if successful, a Regexp object that can be used to match against text.

When matching in a loop, use regexp.Compile instead.

Bad practice

import (
    "regexp"
)

func foo(data []string) {
    for _, s := range data {
        matched, err := regexp.MatchString(`^.*$`, data)
        // ...
    }
}

Recommended

import (
    "regexp"
)

func foo(data []string) {
    re, err := regexp.Compile(`^.*$`)
    if err != nil {
        return
    }

    for _, s := range data {
        matched := re.MatchString(`^.*$`, data)
        // ...
    }
}