Gorilla xmlrpc的小写方法

huangapple go评论69阅读模式
英文:

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(&quot;MyType.lowerCaseMethod&quot;, &quot;MyType.UpperCaseMethod&quot;)

Input with e.g. &lt;methodName&gt;MyType.lowerCaseMethod&lt;/methodName&gt; 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.

huangapple
  • 本文由 发表于 2015年4月9日 00:44:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/29520593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定