How to pass structure to xml-rpc as an argument


I am kinda new to go and have been trying to use XML-RPC methods from my go API, to do that I have selected and everything works fine until I had to pass structure as a parameter. In the example that I got the structure is either python dictionary with format of
{string : string} or empty dictionary. If I give the structure with values that I used in the example on python I would get panic serving.

I have tried to create maps or empty structs but it would say that the given parameters are wrong (the error is caused by xml-rpc). If I would try to make it to json and parse it back using encoding/json decoder it would say that I am using wrong structure.

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","", 0, data, 0)

 if er2 != nil {

The “results” that I am getting now :

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]:
        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, 0xc001008060, 0x1, 0xe, 0x0)
        C:/Users/oplin/go/src/     +0x100d, 0xc, 0xc000239950, 0x7, 0x7, 0xc00016c380)
        C:/Users/oplin/go/src/ +0x1b0, 0x9761ca, 0x3e, 0x95f701, 0xc, 0xc00021f950, 0x7, 0x7, 0x0, 0x0, ...)
        C:/Users/oplin/go/src/ +0x91, 0x3e, 0x95f701, 0xc, 0xc00021f950, 0x7, 0x7, 0x935ce0, 0xc00016c380, 0x0, ...)
     C:/Users/oplin/go/src/ +0x8d, 0xc0001f2010, 0xc00011a300)
        C:/Users/oplin/go/src/ /web/registration.go:208 +0x1a1
net/http.HandlerFunc.ServeHTTP(0x988fb0, 0x1b682c0, 0xc0001f2010, 0xc00011a300)
        C:/Go/src/net/http/server.go:1964 +0x4b*Router).ServeHTTP(0xc0001d0540, 0x1b682c0, 0xc0001f2010, 0xc00011a100)
        C:/Users/oplin/go/src/ +0xd7, 0xc0001f2010, 0xc00011a100, 0xc001008040)
        C:/Users/oplin/go/src/ +0x54, 0x1b682c0, 0xc0001f2010, 0xc00011a100, 0xc001008040)
        C:/Users/oplin/go/src/ +0x55, 0xc00028d340, 0xc00028d3a0, 0x1b682c0, 0xc0001f2010, 0xc00011a100)
        C:/Users/oplin/go/src/ +0xa3, 0xc0001f2010, 0xc00011a100)
        C:/Users/oplin/go/src/ +0x67, 0xc0001f2010, 0xc00011a100, 0xc001008020)
        C:/Users/oplin/go/src/ /lib/middleware.go:13 +0x8f, 0x1b682c0, 0xc0001f2010, 0xc00011a100, 0xc001008020)
        C:/Users/oplin/go/src/ +0x55, 0x988f00, 0xc00028d380, 0x1b682c0, 0xc0001f2010, 0xc00011a100)
        C:/Users/oplin/go/src/ +0xa3*Negroni).ServeHTTP(0xc0002c6690, 0x9f5540, 0xc0003142a0, 0xc00011a100)
        C:/Users/oplin/go/src/ +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


Your fields in Data are unexported, which leads to a panic when trying to access the value using reflect package. That’s because the xmlrpc package tries to access the value of these unexported properties.

Change your Datastruct to export its property:

type Data struct {
  SerialNumber   string
  ProductionDate string

In go, when a type, variable, struct fields, function or methods starts by lowercase, it’s considered unexposed to other packages. To expose it you need to start their name by an uppercase letter. Also, the naming convention in go is CamelCase.

Answered By – Francois

Answer Checked By – Gilberto Lyons (GoLangFix Admin)

Leave a Reply

Your email address will not be published.