从openpgp.Entity中提取rsa.PrivateKey

RSA has several key formats. Is there a way to extract a PKCS 1 private key from a PGP key in golang? Something lik this (it does not work):

var e *openpgp.Entity
e, err := openpgp.NewEntity("test11", "test", "test@test.com", nil)
if err != nil {
    fmt.Println(err)
    return
}

key, ok := e.PrivateKey.PrivateKey.(rsa.PrivateKey)
if !ok {
    // Here is the problem in this solution
    fmt.Printf("Assertation failed")
}

pkcs1PrivateKey := x509.MarshalPKCS1PrivateKey(&key)

privkey_pem := pem.EncodeToMemory(
    &pem.Block{
        Type:  "RSA PRIVATE KEY",
        Bytes: pkcs1PrivateKey,
    },
)
fmt.Printf(string(privkey_pem))

You need to cast to *rsa.PrivateKey and call MarshalPKCS1PrivateKey(key):

key, ok := e.PrivateKey.PrivateKey.(*rsa.PrivateKey)
if !ok {
    // Here is the problem in this solution
    fmt.Printf("Assertation failed")
}

pkcs1PrivateKey := x509.MarshalPKCS1PrivateKey(key)

But this will only make this bit of code work. In production code, you'll need to do a type switch like @Jimb mentioned.

The solution is:

key, ok := e.PrivateKey.PrivateKey.(*rsa.PrivateKey)
if !ok {
    fmt.Printf("Assertation failed")
}

This cast must be a pointer, then the code workes. A type cast is the productive solution: Thanks to @denis-bernard and @JimB!