Golang 未定义的函数

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

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)

huangapple
  • 本文由 发表于 2014年8月9日 12:12:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/25215149.html
匿名

发表评论

匿名网友

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

确定