doukang1962 2014-08-09 04:12
浏览 138
已采纳

Golang未定义函数

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条回答 默认 最新

  • douzai9405 2014-08-09 04:59
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog