英文:
Golang Undefined Functions
问题
我有以下的包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: undefined: 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)
}
英文:
I have the following package src/helpers but when i import it in a different package the only function exported is EmailValidator, but not the other ones, all of them begin with Mayus so i don't know what's happening. thanks
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: undefined: helpers.UniqueNamesValidator
but
func FindByEmail(email *string) (*User, error) {
if email == nil || len(*email) == 0 || !helpers.EmailValidator(*email) {
return nil, errors.New("Invalid Email")
}
...
}
works well
here is the source code.
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)
}
答案1
得分: 1
如此Makefile中所示,并在评论中确认,更好的定义应该是:
GOCMD=go
GOBUILD=$(GOCMD)
build all: $(GOBUILD) $(HELPERS_DIR) $(GOBUILD) $(MODELS_DIR)
英文:
As illustrated in this Makefile, and confirmed in the comments, a better definition would be:
GOCMD=go
GOBUILD=$(GOCMD)
build all: $(GOBUILD) $(HELPERS_DIR) $(GOBUILD) $(MODELS_DIR)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论