duanbimo7212 2019-03-26 14:05
浏览 98
已采纳

如何将结构作为参数传递给xml-rpc

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 https://github.com/mattn/go-xmlrpc 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","10.21.16.160", 0, data, 0)

 if er2 != nil {
  log.Fatal(er2)
 }
}

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]:
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
  • 写回答

1条回答 默认 最新

  • dtvhqlc57127 2019-03-26 14:17
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决