如何将结构作为参数传递给 xml-rpc
How to pass structure to xml-rpc as an argument
我有点新手,我一直在尝试使用 XML-RPC 方法 API,为此我选择了 https://github.com/mattn/go-xmlrpc 并且一切正常,直到我必须将结构作为参数传递。在我得到的例子中,结构是 python 字典,格式为
{string : string} 或空字典。如果我给结构提供我在 python 示例中使用的值,我会得到 panic serving。
我试图创建映射或空结构,但它会说给定的参数是错误的(错误是由 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)
}
}
我现在得到的 "results" :
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
您在 Data 中的字段未导出,这会导致在尝试使用 reflect 包访问该值时出现恐慌。那是因为 xmlrpc 包试图访问这些未导出的属性的值。
更改您的 Data
结构以导出其 属性:
type Data struct {
SerialNumber string
ProductionDate string
}
在 go 中,当类型、变量、结构字段、函数或方法以小写字母开头时,它被认为未暴露给其他包。要公开它,您需要以大写字母开头他们的名字。此外,go 中的命名约定是 CamelCase。
我有点新手,我一直在尝试使用 XML-RPC 方法 API,为此我选择了 https://github.com/mattn/go-xmlrpc 并且一切正常,直到我必须将结构作为参数传递。在我得到的例子中,结构是 python 字典,格式为 {string : string} 或空字典。如果我给结构提供我在 python 示例中使用的值,我会得到 panic serving。
我试图创建映射或空结构,但它会说给定的参数是错误的(错误是由 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)
}
}
我现在得到的 "results" :
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
您在 Data 中的字段未导出,这会导致在尝试使用 reflect 包访问该值时出现恐慌。那是因为 xmlrpc 包试图访问这些未导出的属性的值。
更改您的 Data
结构以导出其 属性:
type Data struct {
SerialNumber string
ProductionDate string
}
在 go 中,当类型、变量、结构字段、函数或方法以小写字母开头时,它被认为未暴露给其他包。要公开它,您需要以大写字母开头他们的名字。此外,go 中的命名约定是 CamelCase。