类型断言错误的原因

func CreateCertificateToMem(template, parent sm2.Certificate, key bccsp.Key) (cert []byte, err error) { pk := key.(gmsm2PrivateKey).privKey pub, := template.PublicKey.(*ecdsa.PublicKey) // wrong type assertion!!! switch pub.Curve { … } 这个函数里的pub, =template.publickey.(ecdsa.Publickey)出现了断言错误,而sm2.certificate的定义如下: type Certificate struct { … PublicKey interface{} … } 调用CreateCertificateToMem的函数如下: sm2PubKey, err := csp.GetSM2PublicKey(priv) template := x509Template() …... sm2cert := gm.ParseX509Certificate2Sm2(&template) sm2cert.PublicKey = sm2PubKey x509Cert, err := genCertificateGMSM2(baseDir, name, sm2cert, sm2cert, sm2PubKey, priv) 而genCertificateGMSM2定义如下: func genCertificateGMSM2(baseDir, name string, template, parent sm2.Certificate, pub sm2.PublicKey, key bccsp.Key) (sm2.Certificate, error) { certBytes, err := gm.CreateCertificateToMem(template, parent, key) …. } 这个类型断言出错的原因是啥?请大侠赐教,非常感谢!

已邀请:

qs

赞同来自:

可以用打印语句打出template.publickey的值

qs

赞同来自:

需要进行两处修改: sm2cert.PUblicKey=*sm2PUbkey pub, := template.PublicKey.(sm2.PublicKey)

要回复问题请先登录注册