我正在使用valyala goRPC 包。我正在尝试实现这个例子:package mainimport ("errors""fmt""log")type ExampleDispatcherService struct {state int}func (s *ExampleDispatcherService) Get() int { return s.state }func (s *ExampleDispatcherService) Set(x int) { s.state = x }func (s *ExampleDispatcherService) GetError42() (int, error) { if s.state == 42 { return 0, errors.New("error42") }return s.state, nil}func (s *ExampleDispatcherService) privateFunc(string) { s.state = 0 }func main() {d := NewDispatcher()service := &ExampleDispatcherService{ state: 123,}// Register exported service functionsd.AddService("MyService", service)// Start rpc server serving registered service.addr := "127.0.0.1:7892"s := NewTCPServer(addr, d.NewHandlerFunc())if err := s.Start(); err != nil { log.Fatalf("Cannot start rpc server: [%s]", err)}defer s.Stop()// Start rpc client connected to the server.c := NewTCPClient(addr)c.Start()defer c.Stop()// Create client wrapper for calling service functions.dc := d.NewServiceClient("MyService", c)res, err := dc.Call("Get", nil)fmt.Printf("Get=%+v, %+v\n", res, err)service.state = 456res, err = dc.Call("Get", nil)fmt.Printf("Get=%+v, %+v\n", res, err)res, err = dc.Call("Set", 78)fmt.Printf("Set=%+v, %+v, %+v\n", res, err, service.state)res, err = dc.Call("GetError42", nil)fmt.Printf("GetError42=%+v, %+v\n", res, err)service.state = 42res, err = dc.Call("GetError42", nil)fmt.Printf("GetError42=%+v, %+v\n", res, err)}但是当我尝试构建它时,我收到了这个错误:PS C:\Work\goRPC> go build .\prueba1.go# command-line-arguments.\prueba1.go:27: undefined: NewDispatcher.\prueba1.go:38: undefined: NewTCPServer.\prueba1.go:45: undefined: NewTCPClient我添加了包,import "github.com/valyala/gorpc"但现在出现此错误:# command-line-arguments.\prueba1.go:7: imported and not used: "git.\prueba1.go:29: undefined: NewDispatcher.\prueba1.go:40: undefined: NewTCPServer.\prueba1.go:47: undefined: NewTCPClient我知道这是一个关于 golang 的 noobie 疑问,但我不知道该怎么做。谢谢你的时间。
- 1 回答
- 0 关注
- 123 浏览
添加回答
举报
0/150
提交
取消