zoukankan      html  css  js  c++  java
  • golang的"..."备忘

    1. 用于数组: 表示长度与元素个数相同.

        在golang中数组的长度是类型的一部分,不同长度,不同类型.

    2. 用于参数: 用于形参表示可变参数. 用于实参表示直接传递. 具体解释参数见官方文档:

        传递可变参数时:

        (1) 如果实参后不跟..., 则在底层创建与形参类型相同的slice,然后将实参赋值后传递.

        (2) 如果实参后跟..., 则不在底层创建与形参类型相同的slice,而是直接将实参传递给形参.

    /ref/spec#Passing_arguments_to_..._parameters

    Passing arguments to ... parameters

    If f is variadic with a final parameter p of type ...T, then within f the type of p is equivalent to type []T. If f is invoked with no actual arguments for p, the value passed to p is nil. Otherwise, the value passed is a new slice of type []T with a new underlying array whose successive elements are the actual arguments, which all must be assignable to T. The length and capacity of the slice is therefore the number of arguments bound to p and may differ for each call site.

    Given the function and calls

    func Greeting(prefix string, who ...string)
    Greeting("nobody")
    Greeting("hello:", "Joe", "Anna", "Eileen")

    within Greeting, who will have the value nil in the first call, and []string{"Joe", "Anna", "Eileen"} in the second.

    If the final argument is assignable to a slice type []T, it may be passed unchanged as the value for a ...T parameter if the argument is followed by .... In this case no new slice is created.

    Given the slice s and call

    s := []string{"James", "Jasmine"}Greeting("goodbye:", s...)

    within Greeting, who will have the same value as s with the same underlying array.

  • 相关阅读:
    浅谈.NET和JAVA的跨平台
    ADO.NET获取TIPTOP存储过程的返回值
    Hide DataGrid Columns via HeaderText
    笑话一则:开车的最高境界
    [推薦]面试时最常问的15问题
    美国小学生守则 VS 中国小学生守则
    Embedded UserControls: Revisited
    SOA认识存误区 详解SOA企业部署的六大关键要素
    Java、.NET,为什么不合二为一?
    [轉]informix语句祥解
  • 原文地址:https://www.cnblogs.com/zolo/p/5849115.html
Copyright © 2011-2022 走看看