简体   繁体   中英

gob error encoding body: gob: type not registered for interface: in golang

During RPC communication, the server cannot calculate the correct value.

I'm getting this error, and I've been thinking about it for a long time.

Can you give any advice

package main

import (
    "crypto/ecdsa"
    "crypto/elliptic"
    "crypto/rand"
    f "fmt"
    "log"
    "net"
    "net/rpc"
)

type Calc int //RPC 서버에 등록하기 위한 임의의 타입정의

type Args struct {
    A, B int
}
type Reply struct { //받을 값
    PrivateKey ecdsa.PrivateKey
    PublicKey  []byte
}

func (c *Calc) Get(args Args, reply *Reply) error {
    private, public := NewKeyPair()
    reply.PrivateKey = private
    reply.PublicKey = public

    return nil
}
func main() {
    rpc.Register(new(Calc))
    ln, err := net.Listen("tcp", ":6000")
    if err != nil {
        f.Println(err)
        return
    }
    defer ln.Close()

    for {
        conn, err := ln.Accept()
        if err != nil {
            continue
        }
        f.Println("Connected")
        defer conn.Close()
        go rpc.ServeConn(conn)
    }
}

func NewKeyPair() (ecdsa.PrivateKey, []byte) {
    curve := elliptic.P256()

    private, err := ecdsa.GenerateKey(curve, rand.Reader)
    if err != nil {
        log.Panic(err)
    }
    pubKey := append(private.PublicKey.X.Bytes(), private.PublicKey.Y.Bytes()...)

    return *private, pubKey
}

---- error ----

2022/06/20 19:58:35 rpc: gob error encoding body: gob: type not registered for interface: elliptic.p256Curve

you can do gob.Register(elliptic.P256())

but this will lead you to next error. Currently its not possible to use gob with elliptic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM