doushuangai9733 2017-10-23 15:09
浏览 142
已采纳

函数返回空字符串,即使它不应该在golang中

I'm having a strange issue.

I have a package named tools in which I have various files with tools for my main package, one of them generates a pseudorandom string that should contain uppercase, lowercase, numerical and certain special characters, to make sure I don't get a string that misses some of the types I did some validations and yet, i seem to miss something because I get an error every now and then

This is my main file:

package main

import (
    "../tools"
    "fmt"
    "strings"
)

const lower = "abcdefghizklmnopqrstuvwxyz"
const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const numrical= "0123456789"
const special = "!#$^*"

func main (){

    for i :=0; i<10; i++ {
        str := tools.GenSpecial(15)
        fmt.Println(str, validate(str))
    }

}

func haslower (s string) bool {
    return strings.ContainsAny(s,lower)
}

func hasupper (s string) bool {
    return strings.ContainsAny(s,upper)
}

func hasnumrical (s string) bool {
    return strings.ContainsAny(s,numrical)
}

func hasspecial (s string) bool {
    return strings.ContainsAny(s,special)
}

func validate (s string) bool {
    return haslower(s) && hasupper(s) && hasnumrical(s) && hasspecial(s)
}

and this is the relevant parts from my tools file:

package tools

import (
    "math/rand"
    "time"
    "strings"
)
const alphanum = 
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const specialchars = 
"abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$^*"
const lower = "abcdefghizklmnopqrstuvwxyz"
const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const numrical= "0123456789"
const special = "!#$^*"

func randomize() {
    rand.Seed(time.Now().UnixNano())
}
func GenSpecial(n int) string { //function to generate a psuedorandom 
alphabetical string with special characters
    rstring := make([]byte, n)
    for i := range rstring {
        randomize()
        rstring[i] = specialchars[rand.Intn(len(specialchars))]
    }
    if validate(string(rstring))&& string(rstring)!=""{
        return string(rstring)
    } else {
        GenSpecial(n)
    }
    return "abc"
}
func haslower (s string) bool {
    return strings.ContainsAny(s,lower)
}

func hasupper (s string) bool {
    return strings.ContainsAny(s,upper)
}

func hasnumrical (s string) bool {
    return strings.ContainsAny(s,numrical)
}

func hasspecial (s string) bool {
    return strings.ContainsAny(s,special)
}

func validate (s string) bool {
    return haslower(s) && hasupper(s) && hasnumrical(s) && hasspecial(s)
}

When I run my main file, i get some values that return the "abc" value, and I don't understand how or why.

Any ideas?

  • 写回答

1条回答 默认 最新

  • dreamact3026 2017-10-23 15:11
    关注

    You are missing a return statement in your else case. If validate returns false, you call GenSpecial and then it returns "abc".

    You want to say:

    if validate(string(rstring))&& string(rstring)!=""{
        return string(rstring)
    } else {
        return GenSpecial(n) // return here!
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化