go version: go1.18.3 ,项目是 mod 模式
在写好demo 执行启动 server时,遇到报错:
![100x](https://static.golangjob.cn/230809/6608bfadd9712aff5b60ff4d2bfdc698.png)
请大神们指点一下迷津!
源码如下:
server 代码:
``` go
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 端代码:
``` go
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)
}
```