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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler