我是一个新手,一直在尝试使用我的go API中的XML-RPC方法,为此我选择了https://github.com/mattn/go-xmlrpc,并且一切正常,直到必须将结构作为参数传递为止。在我得到的示例中,结构是格式为的python字典 {string:string}或空字典。如果我给该结构提供在python示例中使用的值,则会出现恐慌。
我试图创建映射或空结构,但是会说给定的参数是错误的(错误是由xml-rpc引起的)。如果我尝试将其转换为json并使用encoding / json解码器将其解析回去,那说明我使用的是错误的结构。
type Data struct {
serial_number string
production_date string
}
func myFunc(){
data := Data{serial_number: "MB1034RK7856", production_date: ""}
equipmentId, er2 := xmlrpc.Call(url,"addEquipment",19,
"testId", "mac","10.21.16.160", 0, data, 0)
if er2 != nil {
log.Fatal(er2)
}
}
我现在得到的“结果”:
2019/03/26 15:31:20 http: panic serving [::1]:14885: reflect.Value.Interface: cannot return value obtained from unexported field or method
goroutine 66 [running]:
net/http.(*conn).serve.func1(0xc00025a000)
C:/Go/src/net/http/server.go:1746 +0xd7
panic(0x8a6d00, 0x9efd20)
C:/Go/src/runtime/panic.go:513 +0x1c7
reflect.valueInterface(0x8a6d00, 0xc001008060, 0xb8, 0x1, 0xc0010061b0, 0x2a)
C:/Go/src/reflect/value.go:989 +0x1c5
reflect.Value.Interface(0x8a6d00, 0xc001008060, 0xb8, 0xc000239428, 0x1)
C:/Go/src/reflect/value.go:978 +0x4b
github.com/mattn/go-xmlrpc.toXml(0x8f1740, 0xc001008060, 0x1, 0xe, 0x0)
C:/Users/oplin/go/src/github.com/mattn/go-xmlrpc/xmlrpc.go:282 +0x100d
github.com/mattn/go-xmlrpc.makeRequest(0x95f701, 0xc, 0xc000239950, 0x7, 0x7, 0xc00016c380)
C:/Users/oplin/go/src/github.com/mattn/go-xmlrpc/xmlrpc.go:314 +0x1b0
github.com/mattn/go-xmlrpc.call(0xd23a00, 0x9761ca, 0x3e, 0x95f701, 0xc, 0xc00021f950, 0x7, 0x7, 0x0, 0x0, ...)
C:/Users/oplin/go/src/github.com/mattn/go-xmlrpc/xmlrpc.go:322 +0x91
github.com/mattn/go-xmlrpc.Call(0x9761ca, 0x3e, 0x95f701, 0xc, 0xc00021f950, 0x7, 0x7, 0x935ce0, 0xc00016c380, 0x0, ...)
C:/Users/oplin/go/src/github.com/mattn/go-xmlrpc/xmlrpc.go:368 +0x8d
bitbucket.org/epuzzle/puzzle-backend/web.IDontWantToLiveAnymore(0x1b682c0, 0xc0001f2010, 0xc00011a300)
C:/Users/oplin/go/src/bitbucket.org/epuzzle/puzzle-backend /web/registration.go:208 +0x1a1
net/http.HandlerFunc.ServeHTTP(0x988fb0, 0x1b682c0, 0xc0001f2010, 0xc00011a300)
C:/Go/src/net/http/server.go:1964 +0x4b
github.com/gorilla/mux.(*Router).ServeHTTP(0xc0001d0540, 0x1b682c0, 0xc0001f2010, 0xc00011a100)
C:/Users/oplin/go/src/github.com/gorilla/mux/mux.go:212 +0xd7
github.com/codegangsta/negroni.Wrap.func1(0x1b682c0, 0xc0001f2010, 0xc00011a100, 0xc001008040)
C:/Users/oplin/go/src/github.com/codegangsta/negroni/negroni.go:46 +0x54
github.com/codegangsta/negroni.HandlerFunc.ServeHTTP(0xc00028d340, 0x1b682c0, 0xc0001f2010, 0xc00011a100, 0xc001008040)
C:/Users/oplin/go/src/github.com/codegangsta/negroni/negroni.go:29 +0x55
github.com/codegangsta/negroni.middleware.ServeHTTP(0x9f23a0, 0xc00028d340, 0xc00028d3a0, 0x1b682c0, 0xc0001f2010, 0xc00011a100)
C:/Users/oplin/go/src/github.com/codegangsta/negroni/negroni.go:38 +0xa3
github.com/codegangsta/negroni.middleware.ServeHTTP-fm(0x1b682c0, 0xc0001f2010, 0xc00011a100)
C:/Users/oplin/go/src/github.com/codegangsta/negroni/negroni.go:38 +0x67
bitbucket.org/epuzzle/puzzle-backend/lib.Logger(0x1b682c0, 0xc0001f2010, 0xc00011a100, 0xc001008020)
C:/Users/oplin/go/src/bitbucket.org/epuzzle/puzzle-backend /lib/middleware.go:13 +0x8f
github.com/codegangsta/negroni.HandlerFunc.ServeHTTP(0x988f00, 0x1b682c0, 0xc0001f2010, 0xc00011a100, 0xc001008020)
C:/Users/oplin/go/src/github.com/codegangsta/negroni/negroni.go:29 +0x55
github.com/codegangsta/negroni.middleware.ServeHTTP(0x9f23a0, 0x988f00, 0xc00028d380, 0x1b682c0, 0xc0001f2010, 0xc00011a100)
C:/Users/oplin/go/src/github.com/codegangsta/negroni/negroni.go:38 +0xa3
github.com/codegangsta/negroni.(*Negroni).ServeHTTP(0xc0002c6690, 0x9f5540, 0xc0003142a0, 0xc00011a100)
C:/Users/oplin/go/src/github.com/codegangsta/negroni/negroni.go:98 +0xf5
net/http.serverHandler.ServeHTTP(0xc0001c4750, 0x9f5540, 0xc0003142a0, 0xc00011a100)
C:/Go/src/net/http/server.go:2741 +0xb2
net/http.(*conn).serve(0xc00025a000, 0x9f5b80, 0xc0002dc300)
C:/Go/src/net/http/server.go:1847 +0x64d
created by net/http.(*Server).Serve
C:/Go/src/net/http/server.go:2851 +0x2fc
答案 0 :(得分:1)
您的“数据”中的字段未导出,这在尝试使用反射包访问值时导致恐慌。这是因为xmlrpc软件包尝试访问这些未导出属性的值。
更改您的Data
结构以导出其属性:
type Data struct {
SerialNumber string
ProductionDate string
}
在开始时,如果类型,变量,结构字段,函数或方法以小写字母开头,则认为未暴露给其他包。要公开它,您需要以大写字母开头它们的名称。另外,go中的命名约定是CamelCase。