create a password with uppercase letters in go

To create a password with uppercase letters in Go, you can use the math/rand and strings packages.

Here is an example function that generates a password with uppercase letters:

main.go
package main

import (
    "math/rand"
    "strings"
    "time"
)

func main() {
    password := generatePassword(8)
    fmt.Println(password)
}

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

    const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

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

    return string(password)
}
436 chars
26 lines

In this example, we define a charset string that contains all uppercase letters. We then use the rand.Intn function to randomly choose characters from the charset to build our password. Finally, we return the password as a string.

By changing the length parameter in the generatePassword function, you can control the length of the password generated.

gistlibby LogSnag