dqotv26286 2017-11-27 07:56
浏览 69
已采纳

部分使用命名结果参数来设置默认值

Let's say I have a function with 3 return values. I set the values of 2 of them, and want to leave the 3rd one to default if I do not set it. Something like this -

func call(a int) (int, int, r3 string){

  //        return a, a+1, "no error" // stmt 1
  //        return a, a+1             // stmt 2
  //        return                    // stmt 3
}

Running with stmt 2 or stmt 3 uncommented, I get below error -

duplicate argument int
int is shadowed during return

How is int shadowed here? The return list does not have named int params.

Running with stmt 1 uncommented, I get below error -

duplicate argument int
cannot use a (type int) as type string in return argument
cannot use a + 1 (type int) as type string in return argument

Can someone explain origin of these errors ?

Is it not possible to have partial list of named result params (or even returning variables when using named result params) ?

  • 写回答

1条回答 默认 最新

  • donglu9872 2017-11-27 08:25
    关注

    The Go Programming Language Specification

    Function types

    Within a list of parameters or results, the names (IdentifierList) must either all be present or all be absent. If present, each name stands for one item (parameter or result) of the specified type and all non-blank names in the signature must be unique. If absent, each type stands for one item of that type.


    package main
    
    // duplicate argument int
    func call1(a int) (int, int, r3 string) {
        // int is shadowed during return
        return
    }
    
    // duplicate argument int
    func call2(a int) (int string, int string, r3 string) {
        // int is shadowed during return
        return
    }
    
    // duplicate argument int
    func call3(a bool) (int, int, r3 string) {
        // int is shadowed during return
        return
    }
    
    func call4(a int) (int, r2, r3 string) {
        return
    }
    
    func call5(a int) (r1, r2, r3 string) {
        return
    }
    
    func main() {}
    

    Playground: https://play.golang.org/p/PUhY7Y0H9f

    Output:

    tmp/sandbox842451638/main.go:4:33: duplicate argument int
    tmp/sandbox842451638/main.go:6:2: int is shadowed during return
    tmp/sandbox842451638/main.go:10:47: duplicate argument int
    tmp/sandbox842451638/main.go:12:2: int is shadowed during return
    tmp/sandbox842451638/main.go:16:34: duplicate argument int
    tmp/sandbox842451638/main.go:18:2: int is shadowed during return
    

    call1 is shorthand for call2. As you can see, you have duplicate return argument names int: "all non-blank names in the signature must be unique." The first return argument name int is shadowed by the second return argument name int.


    If you want anonymous return arguments, you can use the blank identifier.

    package main
    
    // duplicate argument int
    func callA(a int) (int, int, r3 string) {
        // int is shadowed during return
        return
    }
    
    func callB(a int) (_ int, _ int, r3 string) {
        return
    }
    
    func main() {}
    

    Playground: https://play.golang.org/p/CS1x9mmIh6

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路