doupiao1893
doupiao1893
2017-03-23 20:12

在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 duandu8707 4年前

    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...)
        }
    }
    
    点赞 评论 复制链接分享
  • drddx3115 drddx3115 4年前

    From the Reddit post:

    Unicode. I can tell by the pixels.

    Go doesn't support function overloading. But it does support using Unicode characters in function names, which allows you to write function names that look like other function names.

    The first one is setValue, the second one is setV\u0430lue aka setV\xd0\xb0lue (with CYRILLIC SMALL LETTER A) and the third is setVal\U0001d69ee aka setVal\xf0\x9d\x9a\x9ee (with MATHEMATICAL MONOSPACE SMALL U).

    See also:

    点赞 评论 复制链接分享