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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab