doushuangai9733
2017-10-23 15:09 阅读 109
已采纳

函数返回空字符串,即使它不应该在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 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!
    }
    
    点赞 评论 复制链接分享

相关推荐