Go 中的可选参数? [英] Optional Parameters in Go?

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

问题描述

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

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

推荐答案

Go 没有可选参数 也没有支持方法重载:

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

方法调度被简化,如果它不需要做类型匹配好.使用其他语言的经验告诉我们有各种各样的具有相同名称但的方法偶尔会有不同的签名有用,但它也可能是在实践中令人困惑和脆弱.仅按名称匹配并要求类型的一致性是主要的简化 Go 类型的决策系统.

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.

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

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