goRPC newdispatcher 被导入但未使用

huangapple go评论98阅读模式
英文:

goRPC newdispatcher imported and not used

问题

我正在使用valyala goRPC包进行工作。我正在尝试实现这个例子:

package main

import (
    "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 functions
    d.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 = 456
    res, 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 = 42
    res, 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的初学者问题,但我不知道该怎么办。

谢谢你的时间。

英文:

I'm working with the valyala goRPC package. I'm trying to implement this example:

package main
import (
"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 functions
d.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 = 456
res, 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 = 42
res, err = dc.Call("GetError42", nil)
fmt.Printf("GetError42=%+v, %+v\n", res, err)
}

But when I try to build this I got this error:

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

I added the package import "github.com/valyala/gorpc" but now I get this error:

# 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

I know this is a noobie doubt about golang, but I'm clueless about what to do.

Thanks for your time.

答案1

得分: 2

尝试使用导入前缀来限定标识符,以确保它们被定义。

d := gorpc.NewDispatcher()
英文:

Try to qualify identifiers with import prefix to make them defined

d := gorpc.NewDispatcher()

huangapple
  • 本文由 发表于 2016年4月23日 00:22:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/36798854.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定