我有以下包 src/helpers,但是当我将它导入到不同的包中时,导出的唯一函数是 EmailValidator,但不是其他函数,它们都以 Mayus 开头,所以我不知道发生了什么。谢谢
package models
import (
"helpers"
...
)
func FindByUsername(username *string) (*User, error) {
if username == nil || len(*username) == 0 ||
!helpers.UniqueNamesValidator(*username) {
return nil, errors.New("Invalid Username")
}
...
}
src/models/user.go:88: 未定义: helpers.UniqueNamesValidator
但
func FindByEmail(email *string) (*User, error) {
if email == nil || len(*email) == 0 || !helpers.EmailValidator(*email) {
return nil, errors.New("Invalid Email")
}
...
}
效果很好
这是源代码。
package helpers
import (
"regexp"
)
const (
email_key = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*"
email_domain = "@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
)
func EmailValidator(email string) bool {
pattern := regexp.MustCompile(email_key + email_domain)
return pattern.MatchString(email)
}
func UserNamesValidator(name string) bool {
pattern := regexp.MustCompile(`\A([ña-zA-ZÑ]{3,16} {0,1}){1,3}\z`)
return pattern.MatchString(name)
}
func UniqueNamesValidator(unique_name string) bool {
pattern := regexp.MustCompile(`\A\w{4,10}\z`)
return pattern.MatchString(unique_name)
}
func ProductNameValidator(p_name string) bool {
pattern := regexp.MustCompile(`\A(\w|\s){4,30}\z`)
return pattern.MatchString(p_name)
}
func TextOnlyValidator(text string) bool {
pattern := regexp.MustCompile(`\A(\w+|\s)+\z`)
return pattern.MatchString(text)
}
湖上湖
相关分类