如何在Golang中创建正则表达式数组

huangapple go评论78阅读模式
英文:

How to make array of regexp in golang

问题

我可以帮你翻译代码部分。以下是你要翻译的内容:

patterns := [...]string{`pattern1`, `pattern2`, `pattern3`, ...}
filters := make([]regexp, len(patterns))

for idx, pattern := range patterns {
  filters[idx] = regexp.MustCompile(pattern)
}

...

上述代码无法编译,并给出了"使用未选择的regexp包"的错误。当然,我已经导入了该包,那么创建一个正则表达式数组的正确方法是什么?谢谢。

英文:

I'd like to filter out some paths from given regex patterns with the following code:

patterns := [...]string{`pattern1`, `pattern2`, `pattern3`, ...}
filters := make([]regexp, len(patterns))

for idx, pattern := range patterns {
  filters[idx] = regexp.MustCompile(pattern)
}

...

The above code couldn't be compile, and giving me use of package regexp without selector error, of cause I've import the package already, so, what is the correct way to make an array of regex? thanks.

答案1

得分: 2

尝试创建一个结构体(类型)的数组,而不是一个包的数组:

filters := make([]*regexp.Regexp, len(patterns))
英文:

Try to create an array of struct (a type) not an array of packages:

filters := make([]*regexp.Regexp, len(patterns))

huangapple
  • 本文由 发表于 2021年12月25日 22:22:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/70480638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定