create a password with lowercase letters in go

Here is a simple example of generating a random password with only lowercase letters:

main.go
package main

import (
    "math/rand"
    "time"
)

func main() {
    rand.Seed(time.Now().UnixNano())

    const passwordLength = 10

    // define the characters to use
    const letterBytes = "abcdefghijklmnopqrstuvwxyz"

    // generate a random password
    password := make([]byte, passwordLength)
    for i := range password {
        password[i] = letterBytes[rand.Intn(len(letterBytes))]
    }

    // print the password
    println(string(password))
}
463 chars
25 lines

This will generate a random password with 10 characters using only lowercase letters. The password is created by randomly selecting characters from the letterBytes constant string. We use the math/rand package to generate random numbers and we seed the random number generator with the current time. Finally, we print the generated password to the console.

gistlibby LogSnag