doupiao1893 2017-03-23 20:12
浏览 51
已采纳

在go中重载功能不起作用

I have a function which currently doesn't receive a bool parameter, but then calls another function with a hardcoded bool. We need to remove the hardcoded call and allow a bool to be passed.

I first thought I could try some default parameter - my google searches resulted in that Go apparently doesn't support optional (resp. default) parameter.

So I thought I'd try function overloading.

I found this thread on reddit, which says that it works with a special directive since version 1.7.3: https://www.reddit.com/r/golang/comments/5c57kg/when_did_function_overloading_get_slipped_in/ I am using 1.8, and still I couldn't get it to work.

I am not even sure I may be allowed to use that directive, but I was speculating that changing the function signature right away may be dangerous as I don't know who uses the function...

Anyway - even with //+overloaded it didn't work

Is there any "idiosyncratic" way or pattern to solve this problem in Go?

//some comment
//+overloaded
func (self *RemoteSystem) Apply(rpath, lpath string, dynamic bool) error {
   result, err := anotherFunc(rpath, dynamic)  
}

//some comment
//+overloaded
func (self *RemoteSystem) Apply(rpath, lpath string ) error {
  //in this function anotherFunc was being called, and dynamic is hardcoded to true
   //result, err := anotherFunc(rpath, true)
  return self.Apply(rpath, lpath, true)
}

When I run my test, I get (forgive me for omitting part of the real path to file):

too many arguments in call to self.Apply
    have (string, string, bool)
    want (string, string)
../remotesystem.go:178: (*RemoteSystem).Apply redeclared in this block
    previous declaration at ../remotesystem.go:185
  • 写回答

2条回答 默认 最新

  • duandu8707 2017-03-23 21:15
    关注

    Overloading isn't available in Go. Instead of writing functions with the same name that do different things, it is preferable to be more expressive with what the function does in the function name. In this instance, what would commonly be done is something like this:

    func (self *RemoteSystem) Apply(rpath, lpath string, dynamic bool) error {
        result, err := anotherFunc(rpath, dynamic)  
    }
    
    func (self *RemoteSystem) ApplyDynamic(rpath, lpath string ) error {
        //in this function anotherFunc was being called, and dynamic is hardcoded to true
        return self.Apply(rpath, lpath, true)
    }
    

    Just by the name of the function, you can easily tell what is different and why.

    Another example to provide some context (pun intended). I write a lot of Google App Engine code in Go using go-endpoints. The way to log things is different depending on if you have a context or not. My logging functions ended up like this.

    func LogViaContext(c context.Context, m string, v ...interface{}) {
        if c != nil {
            appenginelog.Debugf(c, m, v...)
        }
    }
    
    func LogViaRequest(r *http.Request, m string, v ...interface{}) {
        if r != nil {
            c := appengine.NewContext(r)
            LogViaContext(c, m, v...)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路