Function 在go中重载函数不起作用

Function 在go中重载函数不起作用,function,go,overloading,Function,Go,Overloading,我有一个函数,它当前没有接收bool参数,但是用硬编码的bool调用了另一个函数。我们需要删除硬编码调用,并允许通过bool 我首先想到我可以尝试一些默认参数——我的谷歌搜索结果表明Go显然不支持可选的resp。默认参数 所以我想我应该尝试函数重载 我在reddit上发现了这个线程,它说从版本1.7.3开始,它与一个特殊指令一起工作: 我正在使用1.8,但仍然无法让它工作 我甚至不确定是否允许我使用该指令,但我猜测立即更改函数签名可能会很危险,因为我不知道谁在使用该函数 无论如何,即使在//+过

我有一个函数,它当前没有接收bool参数,但是用硬编码的bool调用了另一个函数。我们需要删除硬编码调用,并允许通过bool

我首先想到我可以尝试一些默认参数——我的谷歌搜索结果表明Go显然不支持可选的resp。默认参数

所以我想我应该尝试函数重载

我在reddit上发现了这个线程,它说从版本1.7.3开始,它与一个特殊指令一起工作: 我正在使用1.8,但仍然无法让它工作

我甚至不确定是否允许我使用该指令,但我猜测立即更改函数签名可能会很危险,因为我不知道谁在使用该函数

无论如何,即使在//+过载的情况下,它也无法工作

在围棋中有什么特殊的方法或模式来解决这个问题吗

//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)
}
当我运行测试时,请原谅我忽略了文件的部分实际路径:

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
从:

Unicode。我可以通过像素来判断

Go不支持函数重载。但它确实支持在函数名中使用Unicode字符,这允许您编写与其他函数名类似的函数名

第一个是setValue,第二个是setV\u0430lue,又名setV\xd0\xb0lue,带有西里尔字母小写A,第三个是setVal\U0001d69ee,又名setVal\xf0\x9d\x9a\x9ee,带有数学单空间小U

另见:

stackoverflow.com golang.org stackoverflow.com 从:

Unicode。我可以通过像素来判断

Go不支持函数重载。但它确实支持在函数名中使用Unicode字符,这允许您编写与其他函数名类似的函数名

第一个是setValue,第二个是setV\u0430lue,又名setV\xd0\xb0lue,带有西里尔字母小写A,第三个是setVal\U0001d69ee,又名setVal\xf0\x9d\x9a\x9ee,带有数学单空间小U

另见:

stackoverflow.com golang.org stackoverflow.com
在Go中无法使用重载。与其用相同的名称编写做不同事情的函数,不如在函数名中用函数所做的事情更具表现力。在这种情况下,通常会这样做:

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)
}
仅仅通过函数的名称,您就可以很容易地分辨出不同之处以及原因

另一个例子提供了一些上下文双关语。 我使用Go端点在Go中编写了很多Google应用程序引擎代码。记录事情的方式是不同的,这取决于你是否有上下文。我的日志功能最终是这样的

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...)
    }
}

在Go中无法使用重载。与其用相同的名称编写做不同事情的函数,不如在函数名中用函数所做的事情更具表现力。在这种情况下,通常会这样做:

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)
}
仅仅通过函数的名称,您就可以很容易地分辨出不同之处以及原因

另一个例子提供了一些上下文双关语。 我使用Go端点在Go中编写了很多Google应用程序引擎代码。记录事情的方式是不同的,这取决于你是否有上下文。我的日志功能最终是这样的

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...)
    }
}

Go没有函数或方法重载。还要检查引用的reddit topicOK中的第一条注释,因此除了更改函数签名外没有其他方法?可能的解决方案/备选方案的副本:Go没有函数或方法重载。还要检查引用的reddit topicOK中的第一条注释,那么,除了更改函数签名之外,没有其他方法了?可能的解决方案/备选方案的副本:好的,谢谢。这看起来不是解决问题的好办法,但我确实学到了一些东西。一开始读到reddit链接我就不明白。不,使用它一点也不好。原来的帖子是个笑话。好的,谢谢。这看起来不是解决问题的好办法,但我确实学到了一些东西。一开始读到reddit链接我就不明白。不,使用它一点也不好。原来的帖子是个笑话。