英文:
Lowercase methods for Gorilla xmlrpc
问题
我正在使用Gorilla XMLRPC。根据示例代码,我想要将HelloService.Say()
改为helloService.say()
。我已经通过修改注册函数为RPC.RegisterService(new(HelloService), "helloService")
来实现了第一部分。但是我无法将小写方法导出。
我了解到Go语言只允许导出大写方法。那么有没有关于小写方法的解决方法呢?
英文:
I'm using Gorilla XMLRPC. As per the example,
func (h *HelloService) Say(r *http.Request,
args *struct{Who string},
reply *struct{Message string}) error {
reply.Message = "Hello, " + args.Who + "!"
return nil
}
and RPC.RegisterService(new(HelloService), "")
gives me a service HelloService.Say()
. I would like to have helloService.say()
. I was able to get the first part by modifying the register function to RPC.RegisterService(new(HelloService), "helloService")
. But I'm not able to make lowercase methods exported.
I understand golang allows only uppercase methods to be exported. So is there any work-around for lowercase methods?
答案1
得分: 2
我今天遇到了同样的问题。这个问题在几年前的gorilla-xmlrpc中得到了解决。现在你可以在xmlrpc编解码器上注册一个别名:
myCodec := xml.NewCodec()
myCodec.RegisterAlias("MyType.lowerCaseMethod", "MyType.UpperCaseMethod")
使用<methodName>MyType.lowerCaseMethod</methodName>
这样的输入将会被正确识别。
这个函数在godoc中有注释,但在项目的README或相关示例中没有提到。
英文:
I encountered the same issue today. This problem was solved in gorilla-xmlrpc a couple of years ago. Now you can register an alias on the xmlrpc codec:
myCodec := xml.NewCodec()
myCodec.RegisterAlias("MyType.lowerCaseMethod", "MyType.UpperCaseMethod")
Input with e.g. <methodName>MyType.lowerCaseMethod</methodName>
will be recognized as expected.
This function is noted in the godoc but not mentioned in the project README or related examples.
答案2
得分: 1
我认为第一个问题是“为什么需要使用小写的'say'”,第二个问题是,如果需要这样做,为什么要使用Go语言?
这是一个约定,大写的方法和属性是公开的,小写的方法和属性是私有的。
英文:
I think the first question is "why does it need to be [lowercase] "say" and the second question would need to be, if it needed to be that way, why use Go?
It's a convention that Uppercase methods and properties are exported and lowercase methods and properties are private.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论