相关疑难解决方法(0)

可选参数?

Can Go有可选参数吗?或者我可以只定义两个具有相同名称和不同数量的参数的函数?

overloading go

411
推荐指数
11
解决办法
21万
查看次数

在go中重载函数不起作用

我有一个当前没有收到bool参数的函数,但随后用硬编码的bool调用另一个函数.我们需要删除硬编码调用并允许传递bool.

我首先想到我可以尝试一些默认参数 - 我的谷歌搜索导致Go显然不支持可选(resp.default)参数.

所以我想我会尝试函数重载.

我在reddit上发现了这个帖子,它表示它可以使用特殊指令,因为版本1.7.3:https: //www.reddit.com/r/golang/comments/5c57kg/when_did_function_overloading_get_slipped_in/ 我正在使用1.8,但我仍然无法获得它工作.

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

无论如何 - 即使// +重载它也没有用

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)
}
Run Code Online (Sandbox Code Playgroud)

当我运行我的测试时,我得到(原谅我省略了部分真正的文件路径):

too many arguments …
Run Code Online (Sandbox Code Playgroud)

overloading function go

1
推荐指数
1
解决办法
576
查看次数

标签 统计

go ×2

overloading ×2

function ×1