{"id":2535,"date":"2015-11-21T22:56:21","date_gmt":"2015-11-21T14:56:21","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2535"},"modified":"2015-11-21T22:56:21","modified_gmt":"2015-11-21T14:56:21","slug":"%e7%94%a8go%e7%94%9f%e6%88%90%e6%8c%87%e5%ae%9a%e9%95%bf%e5%ba%a6%e7%9a%84%e9%9a%8f%e6%9c%ba%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2535.html","title":{"rendered":"\u7528Go\u751f\u6210\u6307\u5b9a\u957f\u5ea6\u7684\u968f\u673a\u5b57\u7b26\u4e32"},"content":{"rendered":"<p>=Start=<\/p>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<p>golang fixed length random string<\/p>\n<h6>\u53c2\u8003\u89e3\u7b54\uff1a<\/h6>\n<p>\u5e38\u89c4\u7684\u6bd4\u8f83\u5bb9\u6613\u60f3\u5230\u7684\u65b9\u6848\uff08\u4ece\u76ee\u6807\u5b57\u7b26\u96c6\u4e2d\u968f\u673a\u9009\u51faN\u4e2a\u5b57\u7b26\u7ec4\u6210\u5b57\u7b26\u4e32\uff09\uff1a<\/p>\n<pre class=\"lang:default decode:true\">package main\n\nimport (\n    \"fmt\"\n    \"math\/rand\"\n)\n\nvar letters = []rune(\"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\")\n\nfunc randSeq(n int) string {\n    b := make([]rune, n)\n    for i := range b {\n        b[i] = letters[rand.Intn(len(letters))]\n    }\n    return string(b)\n}\n\nfunc main() {\n    fmt.Println(randSeq(10))\n}<\/pre>\n<p>\u4ece\u7b80\u5355\u65b9\u6848\u5f00\u59cb\u8fdb\u884c\u7684\u4e00\u6b21\u4f18\u5316\u5386\u7a0b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">\/\/ \u5c06rune\u6570\u7ec4\u7528byte\u6570\u7ec4\u66ff\u6362\n\/\/ var lettersB = []bytes(\"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\")\n\n\/\/ \u5c06rune\u6570\u7ec4\u7528\u5b57\u7b26\u4e32\u5e38\u91cf\u66ff\u6362\nconst letterBytes = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n\nfunc RandStringBytes(n int) string {\n\tb := make([]byte, n)\n\tfor i := range b {\n\t\tb[i] = letterBytes[rand.Intn(len(letterBytes))]\n\t}\n\treturn string(b)\n}<\/pre>\n<p>=<\/p>\n<pre class=\"lang:default decode:true\">\/\/ \u7528rand.Int63()\u66ff\u6362rand.Intn()\nfunc RandStringBytesRmndr(n int) string {\n\tb := make([]byte, n)\n\tfor i := range b {\n\t\tb[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]\n\t}\n\treturn string(b)\n}<\/pre>\n<p>=<\/p>\n<pre class=\"lang:default decode:true\">\/\/ \u7528\u63a9\u7801\u8fdb\u884c\u66ff\u6362\nconst letterBytes = \"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\"\nconst (\n    letterIdxBits = 6                    \/\/ 6 bits to represent a letter index\n    letterIdxMask = 1&lt;&lt;letterIdxBits - 1 \/\/ All 1-bits, as many as letterIdxBits\n    letterIdxMax  = 63 \/ letterIdxBits   \/\/ # of letter indices fitting in 63 bits\n)\n\nfunc RandStringBytesMaskImpr(n int) string {\n    b := make([]byte, n)\n    \/\/ A rand.Int63() generates 63 random bits, enough for letterIdxMax letters!\n    for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i &gt;= 0; {\n        if remain == 0 {\n            cache, remain = rand.Int63(), letterIdxMax\n        }\n        if idx := int(cache &amp; letterIdxMask); idx &lt; len(letterBytes) {\n            b[i] = letterBytes[idx]\n            i--\n        }\n        cache &gt;&gt;= letterIdxBits\n        remain--\n    }\n\n    return string(b)\n}<\/pre>\n<p>=<\/p>\n<p>\u4e0a\u9762\u7684\u65b9\u6cd5\u7528\u7684\u90fd\u662f&#8221;math\/rand&#8221;\u63d0\u4f9b(\u4f2a)\u968f\u673a\u6570\uff0c\u5982\u679c\u5bf9\u968f\u673a\u6027\u6709\u9ad8\u8981\u6c42\u7684\u8bdd\uff0c\u53ef\u4ee5\u7528&#8221;crypto\/rand&#8221;\u5b9e\u73b0\uff08\u901f\u5ea6\u76f8\u5bf9\u6765\u8bf4\u4f1a\u6162\u4e9b\uff09\uff1a<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Reference: https:\/\/github.com\/dchest\/uniuri\/blob\/master\/uniuri.go\npackage main\n\nimport (\n    \"crypto\/rand\"\n    \"fmt\"\n)\n\nconst (\n    StdLen  = 16\n    UUIDLen = 20\n)\n\nvar StdChars = []byte(\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\")\n\/\/ var AsciiChars = []byte(\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&amp;*()-_=+,.?\/:;{}[]`~\")\n\nfunc main() {\n    fmt.Printf(\"%s\\n\", New())\n    fmt.Printf(\"%s\\n\", New())\n\n    fmt.Printf(\"%s\\n\", NewLen(8))\n    fmt.Printf(\"%s\\n\", NewLen(8))\n}\n\nfunc New() string {\n    return NewLenChars(StdLen, StdChars)\n}\n\nfunc NewLen(length int) string {\n    return NewLenChars(length, StdChars)\n}\n\n\/\/ NewLenChars returns a new random string of the provided length, consisting of the provided byte slice of allowed characters(maximum 256).\nfunc NewLenChars(length int, chars []byte) string {\n    if length == 0 {\n        return \"\"\n    }\n    clen := len(chars)\n    if clen &lt; 2 || clen &gt; 256 {\n        panic(\"Wrong charset length for NewLenChars()\")\n    }\n    maxrb := 255 - (256 % clen)\n    b := make([]byte, length)\n    r := make([]byte, length+(length\/4)) \/\/ storage for random bytes.\n    i := 0\n    for {\n        if _, err := rand.Read(r); err != nil {\n            panic(\"Error reading random bytes: \" + err.Error())\n        }\n        for _, rb := range r {\n            c := int(rb)\n            if c &gt; maxrb {\n                continue \/\/ Skip this number to avoid modulo bias.\n            }\n            b[i] = chars[c%clen]\n            i++\n            if i == length {\n                return string(b)\n            }\n        }\n    }\n}<\/pre>\n<p>=<\/p>\n<p>\u8fd8\u6709\u4e00\u4e2a\u6bd4\u8f83\u504f\u95e8\u7684\u65b9\u6cd5\u5c31\u662f\u7528\u54c8\u5e0c\u503c\u6765\u8868\u793a\u968f\u673a\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre class=\"lang:default decode:true\">\/\/ create random passwd\nfunc createPasswd() string {\n    t := time.Now()\n    h := md5.New()\n    io.WriteString(h, \"ixyzero.com\")\n    io.WriteString(h, t.String())\n    passwd := fmt.Sprintf(\"%x\", h.Sum(nil))\n    return passwd\n}<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/22892120\/how-to-generate-a-random-string-of-a-fixed-length-in-golang\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/22892120\/how-to-generate-a-random-string-of-a-fixed-length-in-golang<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/dchest\/uniuri\/blob\/master\/uniuri.go\" target=\"_blank\">https:\/\/github.com\/dchest\/uniuri\/blob\/master\/uniuri.go<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/a\/12772666\" target=\"_blank\">http:\/\/stackoverflow.com\/a\/12772666<\/a><\/li>\n<li><a href=\"https:\/\/golang.org\/pkg\/crypto\/rand\/#Read\" target=\"_blank\">https:\/\/golang.org\/pkg\/crypto\/rand\/#Read<\/a><\/li>\n<\/ul>\n<p>=EOF=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u641c\u7d22\u5173\u952e\u5b57\uff1a golang fixed length random string \u53c2\u8003\u89e3\u7b54\uff1a  [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[564,347,19],"class_list":["post-2535","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-golang","tag-random","tag-tips"],"views":8528,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2535"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2535\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}