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条)

报告相同问题?