英文:
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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论