go version: go1.18.3 ,项目是 mod 模式
在写好demo 执行启动 server时,遇到报错:
请大神们指点一下迷津! 源码如下: server 代码:
package main
import (
"net"
"net/rpc"
)
type HelloService struct {
}
func (s *HelloService) Hello(request string, reply *string) error {
*reply = "hello " + request
return nil
}
func main() {
listener, _ := net.Listen("tcp", ":1234")
_ = rpc.RegisterName("HelloService", &HelloService{})
for {
conn, _ := listener.Accept() // 监听连接
go rpc.ServeConn(conn)
}
}
client 端代码:
package main
import (
"fmt"
"net/rpc"
)
func main() {
// 1、建立链接 2、
client, err := rpc.Dial("tcp", "localhost:1234")
if err != nil {
panic("connect error")
}
var reply string
client.Call("HelloService.Hello", "superman", &reply)
if err != nil {
panic("remote call failed")
}
fmt.Println(reply)
}
有疑问加站长微信联系(非本文作者)

你给的代码运行没问题,估计是你的环境问题,我看报错中的源码对应行都是注释啊