英文:
Go bug when marshalling data using JSON-RPC - am I being dumb?
问题
所以我正在使用类似以下代码的代码,并且无法使FooHolder中的RPC正常工作,其中它保存符合接口的对象。我可能漏掉了什么,但看起来应该可以工作-使用reflect包时,它正确识别类型,但在函数返回时出现了问题...
package main
import (
"log"
"net"
"net/rpc"
"net/rpc/jsonrpc"
"reflect"
)
type FooInterface interface {
DoTheBartman(in *string, out *string) error
DoThis(in *[]string, out *[]string) error
NoProblems(in *int, out *int) error
}
type Foo struct {
wibble string
}
func (f *Foo) DoTheBartman(in *string, out *string) error {
log.Println("do the bartman")
*out = "^^[ " + *in + "]^^"
return nil
}
func (f *Foo) DoThis(in *[]string, out *[]string) error {
log.Println("Doing this")
log.Println("Type in - ", reflect.TypeOf(in))
log.Println("Type *in - ", reflect.TypeOf(*in))
log.Println("Type out - ", reflect.TypeOf(out))
log.Println("Type *out - ", reflect.TypeOf(*out))
*out = append(*in, "Hello")
return nil
}
type FooHolder struct {
TheFoos []FooInterface
}
func (fh *FooHolder) GetFoos(in *[]Foo, out *[]Foo) error {
log.Println("Getting Foos...")
log.Println("Type in - ", reflect.TypeOf(in))
log.Println("Type *in - ", reflect.TypeOf(*in))
log.Println("Type out - ", reflect.TypeOf(out))
log.Println("Type *out - ", reflect.TypeOf(*out))
return nil // This fails but with a nil... what's up with that.
}
func (f *Foo) NoProblems(in *int, out *int) error {
log.Println("No Problems doing maths")
log.Println("Type in - ", reflect.TypeOf(in))
log.Println("Type *in - ", reflect.TypeOf(*in))
log.Println("Type out - ", reflect.TypeOf(out))
log.Println("Type *out - ", reflect.TypeOf(*out))
*out = 42 - (*in)
return nil
}
// This all works just fine
func startServer(f FooInterface) {
server := rpc.NewServer()
server.Register(f)
l, e := net.Listen("tcp", ":8222")
if e != nil {
log.Fatal("listen error:", e)
}
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
log.Println("Incoming!")
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}
// It starts to go wrong here...
func startFooHolderServer(f *FooHolder) {
server := rpc.NewServer()
server.Register(f)
l, e := net.Listen("tcp", ":8222")
if e != nil {
log.Fatal("listen error:", e)
}
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
log.Println("Incoming!")
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}
func main() {
foo1 := &Foo{}
fooHolder := &FooHolder{}
fooHolder.TheFoos = append(fooHolder.TheFoos, foo1)
// go startServer(foo1)
go startFooHolderServer(fooHolder)
conn, err := net.Dial("tcp", "localhost:8222")
if err != nil {
panic(err)
}
defer conn.Close()
c := jsonrpc.NewClient(conn)
var foo []Foo
log.Println("Type - ", reflect.TypeOf(foo))
log.Println("Type - ", reflect.TypeOf(&foo))
err = c.Call("FooHolder.GetFoos", foo, &foo)
if err != nil {
log.Fatal("RPC error:", err)
}
// No Problems?
var baz int
err = c.Call("Foo.NoProblems", baz, &baz)
if err != nil {
log.Fatal("RPC error:", err)
}
log.Println("Yay - baz is now", baz)
stringArg := "Put stuff around me"
err = c.Call("Foo.DoTheBartman", stringArg, &stringArg)
if err != nil {
log.Fatal("RPC error:", err)
}
log.Println("Yay - stringArg is now", stringArg)
// Also No Problem
bar := append([]string{"Hello"}, stringArg)
log.Println("Type - ", reflect.TypeOf(bar))
err = c.Call("Foo.DoThis", bar, &bar)
if err != nil {
log.Fatal("RPC error:", err)
}
log.Println("Yay - bar is now", bar)
}
输出:
rpc-demo $ go run demo.go
2015/09/05 20:48:32 Incoming!
2015/09/05 20:48:32 Type - []main.Foo
2015/09/05 20:48:32 Type - *[]main.Foo
2015/09/05 20:48:32 Getting Foos...
2015/09/05 20:48:32 Type in - *[]main.Foo
2015/09/05 20:48:32 Type *in - []main.Foo
2015/09/05 20:48:32 Type out - *[]main.Foo
2015/09/05 20:48:32 Type *out - []main.Foo
2015/09/05 20:48:32 RPC error:invalid error <nil>
exit status 1
rpc-demo $
有人遇到过类似的问题吗?如果我声明一个新类型并将其传递,也没有帮助,我看不到任何前进的方法。
*顺便说一句,但可能相关的是,我将一个Go应用程序编译为存档,并将其构建为C程序,然后将接口更改为与Foo而不是Foo接口匹配,这样就可以工作了!奇怪。*
<details>
<summary>英文:</summary>
So I'm working with code similar to the following and can't get the RPC to work in the FooHolder where it holds objects that meets an interface. I might be missing something but it looks like it should work - using the reflect package it correctly identifies the types but there's something wrong when the function returns....
package main
import (
"log"
"net"
"net/rpc"
"net/rpc/jsonrpc"
"reflect"
)
type FooInterface interface {
DoTheBartman(in *string, out *string) error
DoThis(in *[]string, out *[]string) error
NoProblems(in *int, out *int) error
}
type Foo struct {
wibble string
}
func (f *Foo) DoTheBartman(in *string, out *string) error {
log.Println("do the bartman")
*out = "^^[ " + *in + "]^^"
return nil
}
func (f *Foo) DoThis(in *[]string, out *[]string) error {
log.Println("Doing this")
log.Println("Type in - ", reflect.TypeOf(in))
log.Println("Type *in - ", reflect.TypeOf(*in))
log.Println("Type out - ", reflect.TypeOf(out))
log.Println("Type *out - ", reflect.TypeOf(*out))
*out = append(*in, "Hello")
return nil
}
type FooHolder struct {
TheFoos []FooInterface
}
func (fh *FooHolder) GetFoos(in *[]Foo, out *[]Foo) error {
log.Println("Getting Foos...")
log.Println("Type in - ", reflect.TypeOf(in))
log.Println("Type *in - ", reflect.TypeOf(*in))
log.Println("Type out - ", reflect.TypeOf(out))
log.Println("Type *out - ", reflect.TypeOf(*out))
return nil // This fails but with a nil... what's up with that.
}
^^ This is the function that fails, not sure why? It returns a nil (and it's reported as a nil but errors out with a exit 1
func (f *Foo) NoProblems(in *int, out *int) error {
log.Println("No Problems doing maths")
log.Println("Type in - ", reflect.TypeOf(in))
log.Println("Type *in - ", reflect.TypeOf(*in))
log.Println("Type out - ", reflect.TypeOf(out))
log.Println("Type *out - ", reflect.TypeOf(*out))
*out = 42 - (*in)
return nil
}
// This all works just fine
func startServer(f FooInterface) {
server := rpc.NewServer()
server.Register(f)
l, e := net.Listen("tcp", ":8222")
if e != nil {
log.Fatal("listen error:", e)
}
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
log.Println("Incoming!")
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}
// It starts to go wrong here...
func startFooHolderServer(f *FooHolder) {
server := rpc.NewServer()
server.Register(f)
l, e := net.Listen("tcp", ":8222")
if e != nil {
log.Fatal("listen error:", e)
}
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
log.Println("Incoming!")
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}
func main() {
foo1 := &Foo{}
fooHolder := &FooHolder{}
fooHolder.TheFoos = append(fooHolder.TheFoos, foo1)
// go startServer(foo1)
go startFooHolderServer(fooHolder)
conn, err := net.Dial("tcp", "localhost:8222")
if err != nil {
panic(err)
}
defer conn.Close()
c := jsonrpc.NewClient(conn)
var foo []Foo
log.Println("Type - ", reflect.TypeOf(foo))
log.Println("Type - ", reflect.TypeOf(&foo))
err = c.Call("FooHolder.GetFoos", foo, &foo)
if err != nil {
log.Fatal("RPC error:", err)
}
// No Problems?
var baz int
err = c.Call("Foo.NoProblems", baz, &baz)
if err != nil {
log.Fatal("RPC error:", err)
}
log.Println("Yay - baz is now", baz)
stringArg := "Put stuff around me"
err = c.Call("Foo.DoTheBartman", stringArg, &stringArg)
if err != nil {
log.Fatal("RPC error:", err)
}
log.Println("Yay - stringArg is now", stringArg)
// Also No Problem
bar := append([]string{"Hello"}, stringArg)
log.Println("Type - ", reflect.TypeOf(bar))
err = c.Call("Foo.DoThis", bar, &bar)
if err != nil {
log.Fatal("RPC error:", err)
}
log.Println("Yay - bar is now", bar)
}
Output:
rpc-demo $ go run demo.go
2015/09/05 20:48:32 Incoming!
2015/09/05 20:48:32 Type - []main.Foo
2015/09/05 20:48:32 Type - *[]main.Foo
2015/09/05 20:48:32 Getting Foos...
2015/09/05 20:48:32 Type in - *[]main.Foo
2015/09/05 20:48:32 Type *in - []main.Foo
2015/09/05 20:48:32 Type out - *[]main.Foo
2015/09/05 20:48:32 Type *out - []main.Foo
2015/09/05 20:48:32 RPC error:invalid error <nil>
exit status 1
rpc-demo $
Has anyone experienced similar - it doesn't help if I declare a new type and throw that around either and I'm not seeing a way forward.
*Tangentially, but possibly related, I compiled a Go app to archive and built it into a C program before changing the interface to match a Foo rather than a Foo interface and that worked! Weird.*
</details>
# 答案1
**得分**: 1
这是引发错误的代码行:https://github.com/golang/go/blob/master/src/net/rpc/jsonrpc/client.go#L89
这是客户端代码,你可以看到它在`c.resp.Error != nil || c.resp.Result == nil`条件下被执行。
现在你没有返回任何错误,所以问题不在这里。
我认为问题是你没有设置任何结果。`GetFoos`函数将`out`指针保持为nil。将`out`设置为非nil值,应该可以解决问题。
<details>
<summary>英文:</summary>
Here is the line throwing that error : https://github.com/golang/go/blob/master/src/net/rpc/jsonrpc/client.go#L89
It is client-side, and you can see it is reached if `c.resp.Error != nil || c.resp.Result == nil`.
Now you don't return any error, so the problem is not here.
I believe the problem is that you do not set any result. `GetFoos` leaves the `out` pointer nil. Set `out` to a non-nil value and it should work.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论