duanmanpi9358 2016-06-01 13:49
浏览 13
已采纳

片刻之后直接来到…的意思是什么?

What does ... mean in this context in Go?

ids = append(ids[:index], ids[index+1:]...)

I have read this great Q&A: Do three dots (which is called wildcard?) contain multiple meanings?

about what ... means in some situations, but I don't understand what it means in the situation above.

  • 写回答

2条回答 默认 最新

  • douliang2167 2016-06-01 14:27
    关注

    Some languages (C, Python, ...) accept variadic arguments. Basically, you allow the client of a function to pass a number of arguments, without specifying how many. Since the function will still need to process these arguments one way or another, they are usually converted to a collection of some sort. For instance, in Python:

    def foo(x, *args): # * is used for variadic arguments
        return len(args)
    
    >>> foo(1)  # Passed the required argument, but no varargs
    0 
    >>> foo(1, 2, 3)  # Passed the required, plus two variadic arguments
    2
    >>> foo(1, 2, 3, 4, 5, 6) # required + 5 args, etc...
    5
    

    Now one obvious problem of that approach is that a number of arguments is quite a fuzzy concept as far as types are concerned. C uses pointers, Python doesn't really care about types that much in the first place, and Go takes the decision of restricting it to a specified case: you pass a slice of a given type.

    This is nice because it lets the type-system do its thing, while still being quite flexible (in particular, the type in question can be an interface, so you can pass different 'actual types' as long as the function knows how to process these.

    The typical example would be the Command function, which executes a program, passing it some arguments:

    func Command(name string, arg ...string) *Cmd
    

    It makes a lot of sense here, but recall that variadic arguments are just a convenient way to pass slices. You could have the exact same API with:

    func Command(name string, args []string) *Cmd
    

    The only advantage of the first is that it lets you pass no argument, one argument, several... without having to build the slice yourself.

    What about your question then ?

    Sometimes, you do have a slice, but need to call a variadic function. But if you do it naively:

    my_args_slice := []string{"foo", "bar"}
    cmd := Command("myprogram", my_args_slice)
    

    The compiler will complain that it expects string(s), but got a slice instead ! What you want to tell it is that it doesn't have to 'build the slice in the back', because you have a slice already. You do that by using this ellipsis:

    my_args_slice := []string{"foo", "bar"}
    cmd := Command("myprogram", my_args_slice...) // <- Interpret that as strings
    

    The append function, despite being built-in and special, follows the same rules. You can append zero, one, or more elements to the slice. If you want to concatenate slices (i.e. you have a 'slice of arguments' already), you similarly use the ellipsis to make it use it directly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛