create a password with a custom length, uppercase letters and lowercase letters in go

To create a password with a custom length, uppercase letters and lowercase letters in Go, you can use the math/rand and time packages to generate random characters for the password. Here's an example function that creates a password with the specified length and includes both uppercase and lowercase letters:

main.go
import (
    "fmt"
    "math/rand"
    "time"
)

func generatePassword(length int) string {
    rand.Seed(time.Now().UnixNano())

    const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

    password := make([]byte, length)
    for i := range password {
        password[i] = charset[rand.Intn(len(charset))]
    }

    return string(password)
}
365 chars
19 lines

To generate a password with a custom length of 10 characters, you would call the generatePassword function like this:

main.go
password := generatePassword(10)
fmt.Println(password)
55 chars
3 lines

This would output a randomly generated password with 10 characters that includes both uppercase and lowercase letters.

gistlibby LogSnag