Home > Backend Development > Golang > How to generate random string in Golang?

How to generate random string in Golang?

王林
Release: 2024-06-05 17:23:41
Original
1057 people have browsed it

In Golang, random strings can be generated by using the RandStringBytes function in the crypto/rand package. This function accepts two parameters, the first parameter specifies the length of the random string to be generated, and the second parameter is a byte array specifying the range of characters to use.

如何在 Golang 中生成随机字符串?

How to generate random strings in Golang?

Generating random strings in Golang is very simple, just use the RandStringBytes function in the crypto/rand package.

Syntax

func RandStringBytes(n int, alphabet []byte) ([]byte, error)
Copy after login

Where:

  • n Specifies the length of the random string to be generated.
  • alphabet is a byte array specifying the range of characters to use.

Usage

The following is an example of using the RandStringBytes function to generate a random string:

package main

import (
    "crypto/rand"
    "fmt"
)

func main() {
    n := 10
    charset := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
    b, err := rand.RandStringBytes(n, charset)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Printf("随机字符串:%s\n", b)
}
Copy after login
Copy after login

Practical case

Generate a random alphanumeric string of the specified length:

package main

import (
    "crypto/rand"
    "fmt"
)

func main() {
    n := 10
    charset := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
    b, err := rand.RandStringBytes(n, charset)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Printf("随机字符串:%s\n", b)
}
Copy after login
Copy after login

Generate a random lowercase alphanumeric string of the specified length:

package main

import (
    "crypto/rand"
    "fmt"
)

func main() {
    n := 10
    charset := []byte("abcdefghijklmnopqrstuvwxyz")
    b, err := rand.RandStringBytes(n, charset)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Printf("随机字符串:%s\n", b)
}
Copy after login

Generate the specified Random hexadecimal string of length:

package main

import (
    "crypto/rand"
    "fmt"
)

func main() {
    n := 10
    charset := []byte("0123456789abcdef")
    b, err := rand.RandStringBytes(n, charset)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Printf("随机字符串:%s\n", b)
}
Copy after login

The above is the detailed content of How to generate random string in Golang?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template