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

查看:80
本文介绍了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类型的决策 系统.

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天全站免登陆