Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/passphrases/go$ _

Generate Passphrases in Go

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: Go

What: Generate cryptographically secure passphrases in Go using <code>crypto/rand</code> for microservices and backend systems. Idiomatic Go implementation of the Diceware method.

Try it: Use our interactive Passphrases generator or integrate this code into your Go application.

Generate cryptographically secure passphrases in Go using crypto/rand for microservices and backend systems. Idiomatic Go implementation of the Diceware method. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C# , C++ and Ruby or use our interactive web generator.

Go Code Example

package main

import (
    "crypto/rand"
    "fmt"
    "math/big"
    "strings"
)

var wordlist = []string{
    "ability", "able", "about", "above", "accept", // ... 7771 more words
}

func GeneratePassphrase(wordCount int) (string, error) {
    words := make([]string, wordCount)
    max := big.NewInt(int64(len(wordlist)))

    for i := 0; i < wordCount; i++ {
        n, err := rand.Int(rand.Reader, max)
        if err != nil {
            return "", fmt.Errorf("random generation failed: %w", err)
        }
        words[i] = wordlist[n.Int64()]
    }

    return strings.Join(words, "-"), nil
}

func main() {
    // Generate 6-word passphrase
    passphrase, err := GeneratePassphrase(6)
    if err != nil {
        panic(err)
    }
    fmt.Println(passphrase)
    // Output: "crystal-thunder-freedom-mountain-velocity-quantum"

    // Generate multiple passphrases
    for i := 0; i < 3; i++ {
        p, _ := GeneratePassphrase(6)
        fmt.Println(p)
    }
}

[EXPLANATION]

Go's crypto/rand package provides cryptographically secure random number generation using the operating system's CSPRNG. This implementation follows Go conventions with proper error handling. The use of big.Int ensures unbiased random selection across the wordlist.

Expected Output

crystal-thunder-freedom-mountain-velocity-quantum
phoenix-nebula-cascade-brilliant-harmony-infinite
cosmos-radiant-starlight-jupiter-eclipse-zenith

Common Use Cases

  • Microservices authentication
  • API key generation for users
  • Password reset functionality
  • CLI tools and utilities
  • Cloud-native application security

Important Notes

  • Always handle errors from crypto/rand
  • rand.Int() is unbiased and cryptographically secure
  • Load wordlist from embedded file using embed package
  • Consider goroutine-safe global RNG for high-concurrency
  • Use context for cancellable operations

Try Our Interactive Generator

Don't want to write code? Use our free web-based Passphrases generator with instant results.

TRY PASSPHRASES GENERATOR →