dqvj51875 2018-03-01 06:34
浏览 9
已采纳

为什么有时在paren中有返回值,而有时在Go函数签名中却没有?

I noticed that Go function signatures sometimes have a return value in parentheses, and sometimes do not. Is this just a matter of taste, or is there a deeper implication to choosing one over the other.

I've looked through the go documentation, and in go specs in blog articles etc, and not found anything conclusive to answer this question. Most function signature definitions I've come across gloss over this point, and that is why I believe the answer is nuanced.

  func Example(num int) (error){
    ..
  }

  func Example(num int) error {
    ..  
  }
  • 写回答

1条回答 默认 最新

  • douci1851 2018-03-01 06:36
    关注

    If the function has one return value, it means the same thing with or without parentheses. Without parentheses is typically preferred.

    If the function has more than one return value then parentheses are required.

    func Example() (string, error) {
        return "", nil
    }
    

    If there are named return values, parentheses are also required.

    func Example() (s string) {
        s = ""
        return
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测