假设我有一个学生城市列表,其大小可能是 100 或 1000,我想过滤掉所有重复的城市。
我想要一个通用的解决方案,可以用来从任何切片中删除所有重复的字符串。
我是 Go 语言的新手,所以我尝试通过使用另一个循环函数循环并检查元素是否存在来做到这一点。
学生所在城市列表(数据):
studentsCities := []string{"Mumbai", "Delhi", "Ahmedabad", "Mumbai", "Bangalore", "Delhi", "Kolkata", "Pune"}
Run Code Online (Sandbox Code Playgroud)
我创建的函数,它正在完成这项工作:
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func removeDuplicates(strList []string) []string {
list := []string{}
for _, item := range strList {
fmt.Println(item)
if contains(list, item) == false {
list = append(list, item)
}
}
return list
}
Run Code Online (Sandbox Code Playgroud)
我的解决方案测试 …
我检查了 StackOverflow 并找不到任何可以回答如何在 Go Language 中验证电子邮件的问题。
经过一番研究,我想出了并根据我的需要解决了它。
我有这个正则表达式和Go 函数,它工作正常:
import (
"fmt"
"regexp"
)
func main() {
fmt.Println(isEmailValid("test44@gmail.com")) // true
fmt.Println(isEmailValid("test$@gmail.com")) // true -- expected "false"
}
// isEmailValid checks if the email provided is valid by regex.
func isEmailValid(e string) bool {
emailRegex := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
return emailRegex.MatchString(e)
}
Run Code Online (Sandbox Code Playgroud)
问题是它接受了我不想要的特殊字符。我尝试使用一些来自其他语言的“正则表达式”表达式,但它在调试中抛出错误“未知转义”。
谁能给我一个很好的正则表达式或任何适用于 GoLang 的快速解决方案 (pkg)?
我正在尝试测试methods我的 Vue 组件;测试似乎工作正常。但问题是它在控制台中给出折旧警告。
我相信vue-test-utils团队将在下一个主要版本中删除setMethods和属性。methods
我的问题是没有其他方法可以实现为 和 提供的相同setMethods功能methods property。
只是他们提出了一个警告:
To stub a complex method extract it from the component and test it in isolation. Otherwise, the suggestion is to rethink those tests.
Run Code Online (Sandbox Code Playgroud)
我的问题:我们如何提取方法并测试从组件级别单击的方法的功能?
下面是我的简单示例,其中我模拟了一个方法并检查触发单击时是否在组件内部调用它。
const downloadStub = jest.fn()
const wrapper = mount(Documents, {
methods: { downloadNow: downloadStub },
})
it('check download button clicked and calling downloadNow method', () => {
wrapper.find('.download-button').trigger('click')
expect(downloadStub).toBeCalled()
})
Run Code Online (Sandbox Code Playgroud)
注:以上代码运行没有问题;我想知道达到相同结果并避免警告的替代方法?