How to Generate RSA Key Pairs in Go

Generate RSA key pairs in Go using crypto/rand and crypto/rsa packages.

Use the crypto/rand and crypto/rsa packages to generate a private key, then derive the public key from it.

package main

import (
	"crypto/rand"
	"crypto/rsa"
	"fmt"
)

func main() {
	priv, err := rsa.GenerateKey(rand.Reader, 2048)
	if err != nil {
		panic(err)
	}
	pub := &priv.PublicKey
	fmt.Printf("Private Key: %+v\nPublic Key: %+v\n", priv, pub)
}