可选参数? [英] Optional Parameters?

查看:129
本文介绍了可选参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

Go没有可选参数< a href =http://golang.org/doc/faq#overloading =noreferrer>它也不支持方法重载:


如果
不需要像
那样进行类型匹配,则方法调度会被简化。使用其他语言的经验
告诉我们,有多种
方法具有相同的名称,但
不同的签名偶尔会有
有用,但它也可能是
混淆和在实践中很脆弱。
仅通过名称进行匹配并且要求
类型的一致性是Go类型
系统中的一个重要
简化决策。



Can Go have optional parameters? Or can I just define two functions with the same name and a different number of arguments?

解决方案

Go does not have optional parameters nor does it support method overloading:

Method dispatch is simplified if it doesn't need to do type matching as well. Experience with other languages told us that having a variety of methods with the same name but different signatures was occasionally useful but that it could also be confusing and fragile in practice. Matching only by name and requiring consistency in the types was a major simplifying decision in Go's type system.

这篇关于可选参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆